Results 1 to 16 of 16
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default Inner class Static

    Hi all,
    i just went through inner classes in java, and found inner classes cannot hold static variables unless the class declared as static. is there any reason or logic behind that?

    we can find some logic behind all OOP concepts. whats apply here?

    thanks in advance....

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    i just went through inner classes in java, and found inner classes cannot hold static variables unless the class declared as static. is there any reason or logic behind that?

    we can find some logic behind all OOP concepts. whats apply here?
    An object of an inner class is like a planet orbiting a start (an object from the outer class). Should two different planets orbiting different stars have different static variables? If so those variables won't be really static to the inner class, they would be ordinary variables of the outer class. If not, (all static variables should be the same) they're actually static variables of the outer class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks jos,

    you are right. we can place that variable in base class.
    then why there is an option for static inner class?
    what is the use of it?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    thanks jos,

    you are right. we can place that variable in base class.
    then why there is an option for static inner class?
    what is the use of it?
    A static nested class is just a scope: the nested class (not objects thereof) only exist in the scope of the outer class. If it is not a private or protected class it could have been an outer class itself. Nested classes have all access to the static members of the outerclass, that's all and it has its use now and then.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    You might want to check out Roedy Green's page on nested and inner classes. There's nothing on when to use static nested classes, but there's all the syntax laid out including the factoid that you can have static final compiletime constants. He's a bit skeptical about there being any reason for the restriction, but does give some ideas (of others) near the end of the page.

    I have seen reference to static nested classes being used for usage/debugging/testing as part of the .java file of some class, but whose .class files are not shipped with the rest.
    Last edited by pbrockway2; 11-26-2010 at 07:51 AM.

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

    Default

    Sorry - that wasn't clear: you can have static final compile time constants in inner classes, RG is skeptical about the restriction on inner classes having static stuff.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    I have seen reference to static nested classes being used for usage/debugging/testing as part of the .java file of some class, but whose .class files are not shipped with the rest.
    That's cute; do you happen to know how they did that?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Java Code:
    public class MyApp {
        private String recip = "World";
    
        public String getRecip() {return recip;}
    
        public static void main(String[] args) {
            MyApp app = new MyApp();
            System.out.println("Hello " + app.getRecip());
        }
    
        static class Test {
            public static void main(String[] args) {
                MyApp app = new MyApp();
                if(app.getRecip() == null) {
                    System.out.println("Fail!");
                } else {
                    System.out.println("OK");
                }
            }
        }
    }
    
    >javac.exe MyApp.java
    
    >java MyApp$Test
    OK
    
    >del MyApp$Test.class
    
    >java MyApp
    Hello World

    Of course, you don't actually have to delete the test class file(s), but the app runs fine without them so that can be excluded from the jar or whatever.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    Of course, you don't actually have to delete the test class file(s), but the app runs fine without them so that can be excluded from the jar or whatever.
    That's cute, I didn't know that a static nested class isn't loaded when the outer class code is loaded. I guess that no static initializers can be present in the nested class or a reference to it in the outer class can be present; I'll play a bit with it ;-) Thanks for sharing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks to u both for your reply

    what i got from this is

    Inner class instance has existence only with an instance of outer class.
    so inner class holding a static variable is just like saying "an non static variable is static"

    static inner class is just a class put in another class.
    we can made independent instances of it without having outer class instance.

    right?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    thanks to u both for your reply

    what i got from this is

    Inner class instance has existence only with an instance of outer class.
    so inner class holding a static variable is just like saying "an non static variable is static"

    static inner class is just a class put in another class.
    we can made independent instances of it without having outer class instance.

    right?
    Yep, the only thing I don't understand is "an non static variable is static"; see my first reply again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    yes i got it.

    if we need a static variable in inner class then we can meet the same requirement by placing it in outer class.

    also even though inner class is static, its not loaded when outer class is loaded.
    i think inner class is loaded when we refer it first in code...
    right?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    yes i got it.

    if we need a static variable in inner class then we can meet the same requirement by placing it in outer class.

    also even though inner class is static, its not loaded when outer class is loaded.
    i think inner class is loaded when we refer it first in code...
    right?
    Yep, for your last statement you have to check it with pbrockway2, I didn't find the time yet to play with it ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    ok jos thanks alot

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    That's cute, I didn't know that a static nested class isn't loaded when the outer class code is loaded. I guess that no static initializers can be present in the nested class or a reference to it in the outer class can be present; I'll play a bit with it ;-) Thanks for sharing.

    kind regards,

    Jos
    This is how lazy loading of singletons works.
    Java Code:
    public class MySingleton {
        public static class MyHolder {
            private static MySingleton singleton = new MySingleton();
        }
        public MySingleton getInstance() {
            return MyHolder.singleton;
        }
    }
    MyHolder isn't loaded until a call to getInstance...and consequently singleton isn't initialised until then.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    This is how lazy loading of singletons works.
    MyHolder isn't loaded until a call to getInstance...and consequently singleton isn't initialised until then.
    Of course! I just didn't see the idiom; I'll go and bang my head againts a pointy stick and punish myself now ... (*)

    kind regards,

    Jos ;-)

    (*) not really, I'll go and get myself a Grolsch ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  2. how to create static array in class
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 03:33 PM
  3. Please help, need to make my class static.
    By sumak in forum New To Java
    Replies: 1
    Last Post: 04-19-2008, 07:29 AM
  4. Static class
    By eva in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 01:02 PM
  5. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 12:24 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
  •