aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes -doubt on static Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Inner classes -doubt on static" Watch "Inner classes -doubt on static" New topic
Author

Inner classes -doubt on static

Deepak Kumar
Greenhorn

Joined: Jul 21, 2005
Posts: 15
The following code works:

public class Temp {
public void method(){
class Inner{
static final int x = 4;
}

System.out.println(new Inner().x);

}
}

But why not this???


public class newTemp {
class newInner{
static int x = 4;
}


}
}

Pls let me know.
Thanks.
Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
Hi Deepak,
If you declare a non-static inner class then you cannot have static members in that although static final members are ok as they are compile-time constants. However, if you declare a an nested interface in a class then it can have static members as nested interfaces are implicitly static.

Please correct me if i am wrong!!

Regards,
Saurabh
hemamalini s.
Greenhorn

Joined: Oct 17, 2005
Posts: 13
static classes,interfaces,variables and methods are top-level element.They can be nested within static construct or top level classes only
Thanx
 
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: Inner classes -doubt on static
 
Similar Threads
Inner Class
Inner Class
INNER CLASS AGAIN~~~! So terrible
abstract class instantiation
static variables