It helps to look down from time to time and examine the very stuff we walk upon
I was reading code today (yes it would seem this is the only this I read these days), and I found something that puzzled me (and it should not have) : “static class MyClass extends” – I know, I know! I should know what it means and I did take a good guess at it … “it means that the inner class is static and can be initiated from the top level class statically” – right?! Well almost. “okay ..”, I thought, “..what other unique ways of declaring a class do we have in Java? And why do we have them? How do we use them?!”
A little bit of googling let me to a great article on Java World [See JW Article On Static Classes] which talks in detail about the various way in Java Classes can be declared.
The JW Article Summary – Type of Java Class Declarations
There are 2 types of Java Class declarations – top level and inner. Inner classes can be Anonymous, Local, Member and Static Member.
- Top-level Class: The usual way of declaring a Java class, within a file which the name of the class. Cannot declare this static. Class Name and Canonical class name are the same.
- Inner Class: Classes defined within a top-level class
- Anonymous Inner Class:
- No name.
- Instantiated only once.
- You have seen these in Java Swing examples “new ActionListener(){…};” . You were implementing an interface using an anonymous class.
- No class name
- Local Class:
- These are class declarations with a name, just like you would define a top-level class …except …
- …they are declared within a “method block” in a top-level class
- You can create as many instances but only within the method block
- Outside the method you cannot access the class or instantiate it, just like a local var
- Class Name and Canonical class name are the same
- Member Class:
- Just like member variables, these are classes declared within the top-level parent
- …also just like member variables these are accessible through the instance of a parent
- You cannot use the parent class to access these inner-classes, just like you cannot access non-static variables
- Class Name is “Package.Parent.InnerClassName” and Canonical class name are of the form “Package.Parent$InnerClassName”
- Static Member Class:
- You use the static modifier when declaring the class
- Declared within a parent class as an inner class
- Accessible using the parent class or the parent instance (although this is discouraged)
- Class Name is “Package.Parent.InnerClassName” and Canonical class name are of the form “Package.Parent$InnerClassName”
- Anonymous Inner Class:
Hands-on Work
I created 5 top-level classes for each example and had the classes implement an interface which has a method called PrintDetails. I left the implementation of the print to the underlying inner classes and in a test harness I instantiated each of the five top-level classes and added them to a list.
After all the classes were added, I iterated through the list to invoke each interface’s print details. Can you tell me what design pattern I have used here?
The examples and their implementation are given below. The readme.txt is a note that I keep for remember what I have learned about class declarations and static, member and local variables. You can find this information on Sun/Oracle’s Java Fundamental tutorials.
Readme.txt
Implemented the examples for classes ... top-level, Anonymous Inner, Local, Member and Static Member.
Note: "static" does not do anything for the class like it does for the static variables
Class variables vs Object variables: ------------------------------------ -Static keyword makes a variable a Class variable -It is then shared across all instances of the class -Stored in one fixed memory location -Any object can change the value, unless it is final -Class variables can also be manipulated without creating an instance of the class
Static methods: --------------- -to access Class variables or Static variables -can be invoked using the ClassName or using the instance of the class (discouraged) -cannot access instance variables -cannot use 'this' keyword
Constants: ---------- -Variables declared using the "static" modifier and the "final" modifier are constants -Their value cannot be reassigned in the program, compile time error results -Compiler replaces the name of the variable with the value if a primitive type or a String is defined as a constant. This is known as a compile time constant.
Sample Code