Java Basic Tutorial
Java Advance Tutorial
In Java, the static
keyword is used for memory management and applies to variables, methods, and blocks. Anything declared static
belongs to the class rather than any specific object.
static
variable is shared among all objects of a class. It belongs to the class and is initialized only once, at the time of class loading.ClassName.variableName
).class ClassName { static DataType variableName; }
Try it yourself
static
method belongs to the class and does not require an object to be called.ClassName.methodName()
).class ClassName { static ReturnType methodName(Parameters) { // Method logic } }
Try it yourself
static
block is used to initialize static variables or execute code during class loading.main()
method or any static method.class ClassName { static { // Static block logic } }
Try it yourself