Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By Tolls

Thread: Is not enclosing class problem

  1. #1
    jjava7 is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Is not enclosing class problem

    Hi
    first off I know this is quite often asked question but I did not find solution to my case.

    I have classes like this:

    Java Code:
    public class TestClass {
    
    
      class InnerClassA
      {
       class InnerClassB
       {
        int bamount;
    
        InnerClassB()
        {
         bamount = amount; // get amount from TestClass
        }
       }
    
       }
    
       int amount;
    
     InnerClassA.InnerClassB incb;
    
      void setSomeClass(InnerClassA.InnerClassB incb)
      {
    
       this.incb = incb;
      }
    
    }



    Then I create the class and try to set setSomeClass but I get error.
    Java Code:
    TestClass test = new TestClass();
    test.setSomeClass( new TestClass.InnerClassA.InnerClassB()  );  // Error: "is not enclosing class"



    I also tried this but it did not work:
    Java Code:
    test.setSomeClass( test.new TestClass.InnerClassA.InnerClassB()  ); // Error: "cannot resolve symbol TestClass"

    how can I properly create InnerClassB ?

    thx!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is not enclosing class problem

    Try this:

    Java Code:
    TestClass t = new TestClass();
    InnerClassA a = t.new InnerClassA();
    InnerClassB b = a.new InnerClassB();
    Regards,
    Jim
    jjava7 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jjava7 is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Is not enclosing class problem

    Thanks that works!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Is not enclosing class problem

    The reason is, to have an instance of InnerClassA you need an instance of a TestClass in order to create it.
    And to have an instance of InnerClassB, you need an instance of InnerClassA.
    jjava7 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is not enclosing class problem

    Was this an academic exercise? I tend to only use inner classes when the enclosing class is the one that needs it. One example would be
    an iterator (where I also may need to access the enclosing class instance fields). Seldom do I need the qualifying new syntax.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jjava7 is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Is not enclosing class problem

    Quote Originally Posted by jim829 View Post
    Was this an academic exercise? I tend to only use inner classes when the enclosing class is the one that needs it. One example would be
    an iterator (where I also may need to access the enclosing class instance fields). Seldom do I need the qualifying new syntax.

    Regards,
    Jim
    this was not academic exercise. it was just how my class ended up in the program I'm making. In my app the InnerClassA actually has InnerClassB variable. And TestClass has ArrayList<InnerClassA> iclassesA;

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2016, 11:45 PM
  2. Replies: 4
    Last Post: 08-11-2014, 02:11 PM
  3. Replies: 3
    Last Post: 12-01-2010, 06:12 AM
  4. Replies: 1
    Last Post: 06-08-2009, 07:27 AM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •