1.
static defines class level and not instance
level
2.
used for static variables/ static methods/ static
block/ static inner class/ Interface static method(only Java 8 onwards)
Static Variables:
1.
class variable and not object/instance variable
2.
since shared across all instances of Object NOT
THREAD SAFE
3.
usually, used with final keyword
4.
if not private, can be accessed with
ClassName.variableName
private static int i;
private static int i;
Static
Method:
1.
Can access only static variables of the class
and invoke only static methods
2.
Usually utility methods which we want to expose
to other class without creating an instance of the class
private static countA();
private static countA();
Static
Block:
1.
Gets executed when the class gets loaded in the
memory by Java ClassLoader
2.
Used to initialize static variables of the class
3.
Can’t access non-static variables in static block
4.
Can have multiple static blocks in a class
Static
Class:
1.
It is a nested class
2.
Can’t use nested on the outer class
j
Java Static Import:
From Java 1.5 we can import static members methods & variables in another class
import static com.PackageName.ClassName.variableName;
import static com.PackageName.ClassName.methodName; -- no need to give ()
No comments:
Post a Comment