java static methods tutorial

Bodhak java static methods tutorial in this chapter we are going to discuss Working with static members, Static variables, what is need of static variables,

how to access static variables of a class, Static methods what is need of static method, how to access static method of class along with syntax and examples. 

 

 

java static methods tutorial

Working with static members:

Static members are Static Methods and Static Variables.

These methods and variables are also known as class methods and class variables.

While declaring/defining the “static” keyword is used.

Variables and methods marked static belong to the class rather than to any particular instance of the class.

These can be used without having any instances of that class at all.

Only the class is sufficient to invoke a static method or access a static variable.

Class / static variables :-

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

A static variable is shared by all the instances of that class i.e only one copy of the static variable is maintained

Only one copy of class variables are created from the point the class is loaded into the JVM until the class is unloaded, regardless of the number of object instances that are created from that class

If one object instance of that class changes the value of a class variable then all the other instances see the same changed value.

Static variables are stored in static memory.

Static variables are created when the program starts and destroyed when the program stops.

Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.

Class variables can be initialized by an assignment statement. Unassigned declaration of class variables will get the default value as instance variables.

Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.

Static variables can be accessed by calling the class name. ClassName.VariableName.