Static Keyword
Static keyword belongs to class than an object(instance of class) and is used for memory management. Static keyword can be used for:
- Variable
- Method
- Block
- Nested class
1) Static Variable
Variable which is declared as static is called static variable. When you declare any variable as static then
- Static variable start using common property of all objects(which is not unique for each object).For example, when you declare institute name as static then it will be common for all objects using this variable of institute name.
- Static variable makes program memory efficient by using memory only once in the class area at the time of class loading.
Example of static variable
class Student{
int student_rollno;//instance variable
String student_name;
static String college ="GLS ICT";//static variable
//constructor
Student(int no, String name){
student_rollno = no;
student_name = name;
}
void show (){ //method to display the values
System.out.println(student_rollno +" " + student_name +" "+ college);
}
// You can call main method here or can create separate class for it.
}
public class StudentMain{
public static void main(String args[]){
Student stud1 = new Student(10,"Lawrence");
Student stud2 = new Student(20,"Sachin");
Student stud3 = new Student(30,"Indra");
//Student.college="LD"; // This wil change the name of college for all objects of Student class
stud1.show();
stud2.show();
stud3.show();
}
}
Output:
10 Lawrence GLS ICT
20 Sachin GLS ICT
30 Indra GLS ICT
2) Static Method
If you declare method as static,then it becomes static method
- Static method belongs to the class rather than the object
- Static method can be invoked without creating an instance(object) of the class
- Static method can access static data member(i.e static variable) and can change its value
Example 1 of Static Method
class Student{
int rollno;
String name;
static String college = "GLS ICT";
static void modify(){ //you can change value of static variable from static method
college = "LD";
}
Student(int r, String n){ //constructor to initialize the variable
rollno = r;
name = n;
}
void show() //method to display values
{
System.out.println(rollno+" "+name+" "+college);
}
// You can call main method here or can create separate class for it.
}
public class StudentMain{
public static void main(String args[]){
Student.modify();//calling change method
Student s1 = new Student(10,"Lawrence");
Student s2 = new Student(20,"Sachin");
Student s3 = new Student(30,"Indra");
s1.show();
s2.show();
s3.show();
}
}
Output:
10 Lawrence LD
20 Sachin LD
30 Indra LD
Example 2 of Static Method :
class Calculate{
static int square(int x){
return x*x;
}
public static void main(String args[]){
int result=Calculate.square(20);
System.out.println(result);
}
}
Output:
400
Restrictions for the Static Method:
- Static method can not use non static data member or call non-static method directly.
- this and super cannot be used in static context.
class Student{
int rollno=30;//non static
public static void main(String args[]){
System.out.println(a);
}
}
Output: Compile Time Error
3) Static Block
When we named a block as static, then it is called as static block.
- Static Block is used to initialize the static data member.
- Static Block is executed before the main method at the time of classloading
Example of Static Block
class IntroduceYourself{
static{System.out.println("My name is Sachin Ramdas Suryavanshi");}
public static void main(String args[]){
System.out.println("I am an Entrepreneur");
}
}
Output:
My name is Sachin Ramdas Suryavanshi
I am an Entrepreneur
4) Static Nested Class
A class can be made static only if it is a nested class.When a nested class is declared as static,it is called static nested class.
- Nested static class doesn’t need a reference of Outer class.
- Static class cannot access non-static members of the Outer class.
Example of Static Nested Class
public class Counter {
public static int count = 0;
public int id;
public Counter() {
count++;
id = count;
}
public static void totalCount() {
System.out.println("Number of instances: " + count);
}
public void printID() {
System.out.println("Instance ID: " + id);
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
c1.printID();
c2.printID();
c3.printID();
Counter.totalCount();
}
}
Output:
Instance ID: 1
Instance ID: 2
Instance ID: 3
Number of instances: 3
Why is the Java main method static ?
- It is because the object is not required to call a static method. If main method was not static,then JVM will be required to create an object first then call main() method which would had led to the problem of extra memory allocation.
Can we execute a program without main() method ?
- It was possible till JDK 1.6.but after JDK 1.7, it is not possible to execute Java class without the main method
class Student{
static{
System.out.println("static block is invoked");
System.exit(0);
}
}
Output Since JDK 1.6:
static block is invoked
Output After And With JDK 1.7:
Error: Main method not found in class Student, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application