Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt In Static Memeber...Plz Help Me Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Doubt In Static Memeber...Plz Help Me" Watch "Doubt In Static Memeber...Plz Help Me" New topic
Author

Doubt In Static Memeber...Plz Help Me

Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
Why non-static Inner class cannot have static members but
Non-static inner classes can contain final static fields.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Well thangaraj this is because non-static inner class is always associated with an enclosing class instance. So practically it's members should be inaccessible to any code directly without creating an instance of enclosing class. but if non-static inner class is allowed to have static members, then you can do this



but in case of static constants, the constant value is replaced by the compiler with the actual value of the constant



the System.out.println(Outer.Inner.i); will become System.out.println(10);

I think this is the reason, let's see what others have to say...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Doubt In Static Memeber...Plz Help Me
 
Similar Threads
Modelling Inner classes (static/non-static)
Member class can have static variables??
Non-static Inner Class
static variables
Inner class & serialize