Prev Next

Java / static keyword

What is a static variable in Java?

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.

More Related questions...

Show more question and Answers...


Comments & Discussions