Results 1 to 7 of 7
  1. #1
    Humbly is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Smile Why can't a static method create inner class objects

    In the main method of a program. I tried to initialize an inner class but it didn't work. Why does this happen?

    So this doesn't compiles:

    Java Code:
    public class ExampleError {
    
        public static void main (String[] args) {
    
            InnerClass innerClass = new InnerClass();
                 
        }
    
        class InnerClass {
    
        }
    
    }
    But the following does:

    Java Code:
    public class ExampleError {
    
        public static void main (String[] args) {
    
            InnerClass innerClass = new InnerClass();
                 
        }
    
        
    
    }
    
    class InnerClass {
    
    }
    Any help please?
    Last edited by Humbly; 02-12-2011 at 06:28 AM. Reason: forgot something

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Note that in the second example InnerClass is not actually an inner class.

    The thing about inner classes (unlike, say, static nested classes) is that they have an enclosing instance.

    For example suppose I have a class Sequence within which is an inner class Itr (which might implement Iterator). Then Itr has to be an iterator of something ie Itr has to be associated with an instance of Sequence. The associated Sequence instance can be thought of as the context of Itr.

    When, in the first example, you say "InnerClass innerClass = new InnerClass();" the compiler will object because there is no instance of ExampleError of which this InnerClass type is to be associated. In the second example, despite its name InnerClass is just an ordinary type and you can call new InnerClass() any time you like - including within a static method like main().

  3. #3
    Humbly is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks for the explanation. :)

    Can you please give me example code of how innerClass could be associated to ExampleError?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The following compiles without error:

    Java Code:
    public class ExampleError {
    	
        void someMethod() {
                // innerClass will be associated with the instance
                // whose someMethod() was called
            InnerClass innerClass = new InnerClass();
        }
    
        public static void main (String[] args) {
            ExampleError eg = new ExampleError();
    
                // innerClass will be associated with eg
            InnerClass innerClass = eg.new InnerClass();
        }
    
        class InnerClass {}
    }

    I trust you know what you're doing as it's not my intention to give you enough syntacic rope for you to hang yourself on a whacky design. This is the New to Java forum - the shallow end, if you like - and it's not advisable to attempt triple back somersaults here...
    Last edited by pbrockway2; 02-12-2011 at 07:20 AM.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Here's why it's invalid:

    Java Code:
    public class ExampleError {
    
        int memberVariable;
    
        public static void main (String[] args) {
    
            InnerClass innerClass = new InnerClass();
                 
        }
    
        class InnerClass {
    
          InnerClass() {
            memberVariable++;
          }
    
        }
    
    }
    As you can see, an inner class has access to the parent class' members. It's really just a sub-namespace of the parent class. However, you can make a static inner class, and then it would be available to static methods.

    Java Code:
    public class NoLongerAnError {
    
        public static void main (String[] args) {
    
            InnerClass innerClass = new InnerClass();
                 
        }
    
        static class InnerClass {
    
        }
    
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    It's a nitpick but, just to keep the terminology straight, such static nested classes are not properly referred to as inner.

  7. #7
    Humbly is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks, I understand now.

    I had just never used inner classes much before.

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  2. referencing objects from static method
    By talktofrank in forum New To Java
    Replies: 4
    Last Post: 10-26-2009, 07:09 PM
  3. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 04:17 AM
  4. Static Builder and Create Methods for simple class
    By Pilot Ace in forum New To Java
    Replies: 3
    Last Post: 08-30-2009, 11:18 AM
  5. how to create static array in class
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 04:33 PM

Tags for this Thread

Posting Permissions

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