8 Replies Latest reply: Jan 21, 2008 3:38 PM by 807603 RSS

    inner class instantiation

    807603
      I have a class OptionSet, And OptionSet has an inner class Options, Options class only has two variables, name and price.

      Ok, here is the problem I'm facing. I need to initiate an array object of Options of length k, but I don't know how to write it since it's an inner class, I couldn't find it anywhere.

      If Options is an regular class, I can declare this:
      Options[] opts = new Options[k];
      right?


      I can :

      OptionSet[] zOptionSets = new OptionSet;

      and OptionSet.Options ops = ztwOptionSets.new Option(); can be use to instantiate one Options object ops.

      but this is only for one option, how to initiate an array of options with length k(size k)?

      I tried:
      OptionSet.Options[] opss = ztwOptionSets.new Options()[k];

      OptionSet.Options[] opss = ztwOptionSets.new Options[k];

      They all look wierd to me. Is there any way to instantiate an array of Options of size k from outside?

      Edited by: snowmoom on Jan 20, 2008 11:06 PM
        • 1. Re: inner class instantiation
          807603
          OptionSet.Options[] obj = new OptionSet.Options[k];
          // For instantiating the above array elements you need to have an instance of outer class.
          OptionSet oSet = new OptionSet();
          obj[0] = oSet.new Options();
          //---- so on----
          • 2. Re: inner class instantiation
            807603
            All the objects cannot be initialize at one go since array contains only the references of Option class.

            As per my junk knowlege, we have to initialize each element of array explicitly.
            public class testOptionSet {
            
                 
                 public static void main(String[] args) {
                      testOptionSet outopt = new testOptionSet();
                      int k = 5;
                      testOptionSet.Option []innoptarr = new testOptionSet.Option[k];
                      for(int i=0 ;i<k ; i++)
                      {
                           innoptarr[i] = outopt.new Option();
                      }
                      
                      for(int i=0 ;i<k ; i++)
                      {
                           innoptarr.printinner();
                      }
                      
                 }

                 void print()
                 {
                      System.out.println("hi");
                 }
                 
                 class Option
                 {
                      void printinner()
                      {
                           System.out.println("hi frm inner");
                      }
                 }
            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
            • 3. Re: inner class instantiation
              807603
              Whenever you initialise an array of objects you must put the objects into the array explictly. If the list of options is fixed you can do it with an array initialiser like;
              private Option[] options = new Option[] {
                  new Option("xxxxx"... ),
                  new Option("yyyy" ...)
              };
              Ideally avoid the <reference>.new syntax by creating all your Object instances inside the OptionSet class (where the outer class reference is implicit). If you can't do that is suggests that your option probably shouldn't be an inner class.
              • 4. Re: inner class instantiation
                807603
                Rambhupal,

                I tried the way you wrote above, and it worked exactly right. Thanks very much�I

                snowmoon

                Edited by: snowmoom on Jan 21, 2008 1:12 PM
                • 5. Re: inner class instantiation
                  807603
                  Wacky_aks,


                  I tried you way, it didn't work. But you program passed compiler. I have to check out why mine couldn't pass compiler. Hmm....

                  Thanks very much,

                  snowmoon
                  • 6. Re: inner class instantiation
                    807603
                    malcolmmc,

                    That's a good point. By the way, this is an asignment to let us handle the inner classes. So I have to do it this way. But I'll remember what you said here.

                    Thanks!

                    snowmoon
                    • 7. Re: inner class instantiation
                      807603
                      Ideally avoid the <reference>.new syntax by creating all your Object instances inside the OptionSet class (where the outer class reference is implicit). If you can't do that is suggests that your option probably shouldn't be an inner class.
                      A common pattern is that the inner class implements a relevant interface or have a relevant super class, and the outer class provides a factory method:
                      interface Something {
                      }
                      
                      public class Outer {
                          private class Inner implements Something {
                          }
                      
                          public Something createSomething() {
                              return new Inner();
                          }
                      }
                      You see this in the Collections Framework, for example. It's easy for a client to use:
                      Outer o = new Outer();
                      Something a = o.createSomething();
                      The client doesn't know and doesn't care about the details of Inner.
                      • 8. Re: inner class instantiation
                        807603
                        DrLaszloJamf ,

                        It make sense. I guess the more I explore Java, the more I will understand it.

                        Thanks,

                        lucky snowmoon