Sunday, December 21, 2014

Java static class

In this example, we will discuss about static classes in Java. First of all, let’s give a short explanation of the static modifier. For example, if a field or a method in a class has the static modifier in its declaration , then it is always associated with the class as a whole, rather than with any object of the class.

In the code below we have declared a class named Vehicle, a class field member named vehicleType and a method named getVehicleType(), both declared as static.

ONLY nested classes can be static

The main difference is that inner class requires instantiation of the outer class so as to be initialized and it is always associated with an instance of the enclosing class. On the other hand nested static class is not associated with any instance of the enclosing class. Nested static classes are declared with the static keyword, which means than can be accessed like any other static member of class, as we shown before.

http://stackoverflow.com/questions/7486012/static-classes-in-java

No comments:

Post a Comment