Explain the Java Static Modifier
May 13th, 2008Static Variables or Class Variables
- Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.
- Any java object that belongs to that class can modify its static variables.
- Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly.
- Static variables can be accessed by java instance methods also.
- When the value of a constant is known at compile time it is declared ‘final’ using the ’static’ keyword.
Static Methods or Class Methods
- Similar to static variables, java static methods are also common to classes and not tied to a java instance.
- Good practice in java is that, static methods should be invoked with using the class name though it can be invoked using an object. ClassName.methodName(arguments) or objectName.methodName(arguments)
- General use for java static methods is to access static fields.
- Static methods can be accessed by java instance methods.
- Static methods cannot access instance variables or instance methods directly.
- Static methods cannot use the ‘this’ keyword.
Static Classes
- For java classes, only an inner class can be declared using the static modifier.
- For java a static inner class it does not mean that, all their members are static. These are called nested static classes.



[...] coming to the static import part. Like the above ugly line we have been unknowingly using (abusing) the java static [...]
What is a static import i&hellip on November 17th, 2009 11:34 pmGood, concise answer.
But, I would want one more thing to be added to it:
Static methods can’t be overridden, which implies they are final methods. Rather, the concept of overridden itself should never arise because static methods are part of a class and not of an object.
But, because of the notation we abuse by writing obj.staticMethod(), people generally get confused.
Thanks,
hi…!
If you would use static methods and fields only, you would end up programming procedural like you would do with C or Pascal, loosing all the benefits of oop..so static method is used only when required and instance method is used elsewhere to get the benefit of oop.