How do I access methods inside of a nested class?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: How do I access methods inside of a nested class?

  1. #1
    Join Date
    May 2005
    Posts
    75

    How do I access methods inside of a nested class?

    Say I have this code...

    Code:
    public class SuperClass {
    
         // nested class
         public class InnerClass{
    
              public boolean isTrue(){
    
                   return boolean;
    
              }
    
         }
    
    }
    Code:
    public class SubClass extends SuperClass{
    
         // stuff
    
    }
    Code:
    public class Tester  {
    
         SubClass object = new SubClass();
    
         // How do I make the following line work?
         System.out.println(object.isTrue());
    
    }
    Thanks.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    As far as I know, you can't. Nor can you access that method if you created an instance of SuperClass. The only way that you can access that method is if you create an instance of InnerClass.

  3. #3
    Join Date
    May 2005
    Posts
    75
    I can create an instance of InnerClass from inside Super or Sub class, but not the Tester class. I need to if there is any way to either create an instance of InnerClass from within Tester, or to simply access its methods.

  4. #4
    Join Date
    Jan 2006
    Location
    Sydney
    Posts
    49

    Solution

    Code:
    public class Tester  {
         SuperClass.InnerClass object = new SuperClass().new InnerClass();
         SubClass.InnerClass object2 =  new SubClass().new InnerClass();
         public  void myTest1() {
             // How do I make the following line work?
             System.out.println(object.isTrue());
             
             System.out.println(object2.isTrue());
         }
         
        public static void main(String[] args) {
            new Tester().myTest1();
        }
    }
    
    public class SuperClass {
        // nested class
        public class InnerClass{
             public boolean isTrue(){
                  boolean isTrue = false;	    
                  return isTrue;
    
             }
        }
    }
    
    public class SubClass extends SuperClass{
        // stuff
    }

    Your nested class is non-static, so it is only meaningful in the instance of your outer class. Hope this helps.
    Arul

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  2. calling parent class methods from subclasses
    By Nate McFeters in forum Java
    Replies: 0
    Last Post: 12-07-2001, 08:06 PM
  3. Replies: 1
    Last Post: 10-20-2001, 06:27 PM
  4. Calling base class methods
    By GR in forum Java
    Replies: 1
    Last Post: 11-09-2000, 06:38 PM
  5. Using Class inside session onstart
    By New Bie in forum ASP.NET
    Replies: 0
    Last Post: 08-30-2000, 05:05 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center