Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: please tell me how to creat an array of an inner cclass;

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default please tell me how to creat an array of an inner cclass;

    hello,
    please tell me how to creat an array of an inner cclass;

    class outer 
    {
    ....
    ....
             class inner
              {
                ........
                ........
              }
    }
     
    outer o =new outer();
    outer.inner [ ] i = o.new inner[5];
    is this correct??


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: please tell me how to creat an array of an inner cclass;

    For an array, no.
    The correct syntax would be:
    outer.inner[] i = new outer.inner[5];

    However, if you only wanted a single instance of the inner class, something similar to your syntax would work:

    outer o = new outer();
    outer.inner i = o.new inner();

    If you're unsure if something is correct, you can always try it out.

    Something I would recommend is to simply declare the inner class static

    public class Outer
    {
        static class Inner
        {}
    }

    To create a new array it would use the same syntax, but to create a new instance of Inner, you just need to do this:

    Outer.Inner i = new Outer.Inner();
    Last edited by helloworld922; December 22nd, 2010 at 11:55 PM.

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: please tell me how to creat an array of an inner cclass;

    thank you very much
    prima

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. how to creat one source multiple receiver
    By bhatti in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2010, 04:48 PM
  3. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  4. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  5. Creat a tree from list of strings
    By ayri in forum Loops & Control Statements
    Replies: 0
    Last Post: May 10th, 2010, 10:50 AM