aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static inner class" Watch "static inner class" New topic
Author

static inner class

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I studied the static inner class and concluded these. Anyone please correct me if I am wrong.
1. when modifier "static" applying to a class, it can only be applied to a class nested in another class.
2. a static inner class can contain non-static varibles and methods.
3. To access the static methods in a static class, use the class name or class instance, to access the non-static methods in a static class, use class instance.
Thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

1. when modifier "static" applying to a class, it can only be applied to a class nested in another class.

From the JLS, §8.1.1 Class Modifiers:

The access modifier static pertains only to member classes (�8.5, �9.5).

So, yeah, that's right.

2. a static inner class can contain non-static varibles and methods.

From the JLS, §8.1.2 Inner Classes and Enclosing Instances:

Inner classes may inherit static members that are not compile-time constants even though they may not declare them. Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language. Member interfaces (�8.5) are always implicitly static so they are never considered to be inner classes.

So that's right, too.

3. To access the static methods in a static class, use the class name or class instance, to access the non-static methods in a static class, use class instance.

That's how it is for all static methods and instance methods, regardless of whether or not that class is nested.

One other note - a member class that is given the attribute static is not considered an inner class. Rather, it is called a top-level nested class. A true inner class must have an instance of the enclosing class associated with it - a top-level nested class does not.
I hope that helps,
Corey


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static inner class