Java Static keyword

Static keyword mainly used for memory management in java. It can apply with variables, blocks, methods, and nested class. It belongs to the class than an instance of the class.

Static keyword can be used in java:

  • variable (called as class variable)
  • method (called as class method)
  • block
  • nested class
Java Static variable

When a variable is declared static, then it is called a static variable.

  • It is used to refer to the common property of all objects. Like, school name of students, company name of employees, etc. Name of the school is common for all the students.
  • It allocates memory only once in the class area at the time of class loading.
Advantage

It saves memory, so it is memory efficient for the program.

Why we use static keyword

When we store the details of all the students any college, then student id is unique for every student, but the college name is same. When we create a static variable as a college name, then only once memory is allocated a memory space each time for every student.

Syntax to declare a static variable

public static variable name;

Syntax to declare a static method
		Public static void methodname()
		{
		….
		….
		}
Syntax to access static method and static variable

className.variableName=20;

className.methodName();

Example of static variable
		class Student
		{
		int roll_no;
		String name;
		static String College_Name="MNIT";
		}
		class StaticDemo
		{
		public static void main(String args[])
		{
		Student s1=new Student();
		s1.roll_no=54;
		s1.name="Raj";
		System.out.println(s1.roll_no);
		System.out.println(s1.name);
		System.out.println(Student.College_Name); 
		Student  s2=new  Student();
		s2.roll_no=90;
		s2.name="Geeta";
		System.out.println(s2.roll_no);
		System.out.println(s2.name);
		System.out.println(Student.College_Name); 	
		}
		}
Output

54

Raj

MNIT

90

Geeta

MNIT

Java Static Method

When we apply a static keyword with any method, then it called a static method. It belongs to the class rather than the object of a class. It can be invoked without the need for creating an instance of a class. It accesses static data member and the change the value of it.

Example of static method
		class Student{
		int rollno;
		String name;
		static String college = "BBS";
		static void change(){
		college = "DU";
		}
		Student(int r, String n){
		rollno = r;
		name = n;
		}
		void display(){System.out.println(rollno+" "+name+" "+college);}
		}
		public class TestStaticMethod{
		public static void main(String args[]){
		Student.change();//calling change method
		Student s1 = new Student(101,"Raj");
		Student s2 = new Student(202,"Geeta");
		Student s3 = new Student(303,"Rahul");
		s1.display();
		s2.display();
		s3.display();
		}
		}
Output

101 Raj DU

202 Geeta DU

303 Rahul DU

Conditions of static method
  • It cannot use non-static data member or cannot call the non-static method directly.
  • this and super keyword cannot use in static context.
NOTE:

Java main method is static because to call static method object is not required. When there is a non-static method JVM creates an object first then call main() method.

Java Static Block

It is a set of the statement, which is used to initialize static data member. JVM executes it before main method at the time of class loading.

Syntax
		Static
		{
		….
		//Set of statement
		….
		}
Example
		class Block{  
		  static{System.out.println("static block call");}  
		  public static void main(String args[]){  
		   System.out.println("Hello main method");  
		   }  
		}
Output

static block call

Hello main method

NOTE:

We cannot execute the program without main() method because in previously the only way was a static block, but it was not possible. So it is not possible to execute a java class without the main method.

Copyright 1999- Ducat Creative, All rights reserved.