Java / static keyword
static is a keyword in Java that can be applied to a member variable (class variable), Java method, nested class, and block.
static keyword cannot be applied on top-level class while will result in compile time error.
No. static keyword may be applied for inner class.
If you declare any variable as static, it becomes static variable or class variable.
The static variable is at class level rather than object level. If Class A has a static int variable counter and A has two instances a1 and a2 both will have a static variable counter whose value would be always same. Both instances share the same copy of static int variable.
A Static variable gets memory allocated only once during the time of class loading. A static variable can be accessed directly by calling ClassName.StaticVariableName without an instance for the class.
Static variables help writing memory efficient programs at only one copy of the variable being shared across all the class instances.
No. Unlike C/C++, static local variables are NOT allowed in Java.
A static block, is a block of code inside a Java class that will be executed when a class is first loaded into the JVM. Mostly the static block will be used for initializing the variables.
Static block will be called only one while loading and it cannot have any return type, or any keywords (this or super).
It is also called as Initializer block.
class MyClass { static int val; static{ val = 100; } }
Yes, we can have more than one static block in a Java class. It will be executed exactly in the same order it appears.
When you declare a static method to be abstract since the static method can be called directly using the class reference, making it abstract would make it possible to call an undefined method which is of no use, hence it is NOT allowed.
A static method belongs to class rather than the object. It can be called directly by using the class name. A static method can access static variables directly and it cannot access non-static variables and can only call a static method directly and it cannot call a non-static method from it.
Nested static class in Java is a static member of any top-level class. We may create the instance of the nested static class without the instance of enclosing class.
public class StaticNestedClass{ public static void main(String args[]){ StaticNestedClass.NestedStaticClass ns = new StaticNestedClass.NestedStaticClass(); System.out.println(ns.getDesc()); } static class NestedStaticClass{ public String desc =" Example of Nested Static Class"; public String getDesc(){ return desc; } } }
when we want a single resource to be shared between all instances and normally we do this for utility classes which are required by all components and which itself doesn't have any state.
No. Unlike static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class; it can use them only through an object reference.
Static nested class can define its own static member while the inner class cannot declare static members.
An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. in Static nested class, it can exist even without an instance of enclosing class.
public class StaticNestedClass{ int nonstaticInt; static int staticInt; public static void main(String args[]){ StaticNestedClass.NestedStaticClass ns = new StaticNestedClass.NestedStaticClass(); System.out.println(ns.getDesc()); } static class NestedStaticClass{ static public String desc =" Example of Nested Static Class"; public String getDesc(){ //System.out.println(nonstaticInt); //Compilation error System.out.println(staticInt); return desc; } } class NestedNonStaticClass{ public String desc =" Example of Inner Class"; public String getDesc(){ System.out.println(nonstaticInt); System.out.println(staticInt); return desc; } } }
Forward reference error is a Java compile time error that occurs when a variable is referenced before it is declared.
Static block executes first then the initializer block and constructor.
Static variables are always in the scope of the ClassLoader, in most cases per JVM. Each JVM will have its own instance.