When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables.
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static
modifier.
When a member is declared static
, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main()
. main()
is declared as static because it must be called before any objects exist.
static modifier in Java is applicable for the following:
- Blocks
- Variables
- Methods
- Nested Classes
The static
keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.
Static variables are also known as class variables. Local variables cannot be declared static.
The static
keyword is used to create methods that will exist independently of any instances created for the class.
Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.
Methods declared as static have several restrictions:
- They can only directly call other
static
methods. - They can only directly access static data.
- They cannot refer to
this
orsuper
in any way. (The keywordsuper
relates to inheritance and is described in the next section.)
Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.
//Java Program to demonstrate the use of a static method.
class Student{
int rollno;
String name;
//static variable
static String college = "ITS";
//static method to change the value of static variable
static void change(){
college = "BBDIT";
}
//constructor to initialize the variable
Student(int r, String n){
rollno = r;
name = n;
}
//method to display values
void display(){
System.out.println(rollno+" "+name+" "+college);
}
}
//Test class to create and display the values of object
public class TestStaticMethod{
public static void main(String args[]){
Student.change();//calling change method
//creating objects
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method
s1.display();
s2.display();
s3.display();
}
}
111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
Instance initializer block works are used to initialize the properties of an object.
- It is invoked before the constructor is invoked.
- It is invoked every time an object is created.
- They are typically placed above the constructors within braces.
- We can also have multiple Initializer Blocks in a single class. If compiler finds multiple Initializer Blocks, then they all are executed from top to bottom i.e. the Initializer Blocks which is written at top will be executed first.
- You can have Initializer Blocks in parent class also. Iinitializer block code runs immediately after the call to
super()
in a constructor. The compiler executes parents class’s Initializer Blocks before executing current class’s Initializer Blocks.
// Java program to illustrate
// Initializer Block
// with super()
//Parent Class
class B {
B(){
System.out.println("B-Constructor Called");
}
//initializer block
{
System.out.println("B-Initializer Block");
}
}
// Child class
class A extends B {
A(){
super();
System.out.println("A-Constructor Called");
}
//initializer block
{
System.out.println("A-Initializer Block");
}
// main function
public static void main(String[] args){
A a = new A();
}
}
B-Initializer Block
B-Constructor Called
A-Initializer Block
A-Constructor Called
Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded. There can be multiple static initialization blocks in a class that is called in the order they appear in the program.
class JavaExample{
static int num;
static String mystr;
//static initializer block
static{
num = 97;
mystr = "Static keyword in Java";
}
public static void main(String args[]){
System.out.println("Value of num: "+num);
System.out.println("Value of mystr: "+mystr);
}
}
Value of num: 97
Value of mystr: Static keyword in Java