How to Java programming- Static keyword

The static word can be used to attach a Variable or Method to a Class. The variable or Method that is marked static belongs to the Class rather than to any particular instance.The static keyword can be used in 3 scenarios:

1.Static variables
2.Static methods
3.Static blocks of code.
4. Nested Class.


1.Static variables

Static variables are also known as Class Variables. Such variables get default values based on the data type.
Data stored in static variables is common for all the objects or instances of that Class.
Memory allocation for such variables only happens once when the class is loaded in the memory.
These variables can be accessed in any other class using the class name.
Unlike non-static variables, such variables can be accessed directly in static and non-static methods.

Java Static Variables
	 class Example1{
	   static int Var1=77; //Static integer variable
	   String Var2;

	   public static void main(String args[])
		  Example1 ob1 = new Example1();
		  Example1 ob2 = new Example1();
		  ob1.Var2="I'm Object1";
		  ob2.Var2="I'm Object2";
		  System.out.println("ob1 integer:"+ob1.Var1);
		  System.out.println("ob1 String:"+ob1.Var2);
		  System.out.println("ob2 integer:"+ob2.Var1);
		  System.out.println("ob2 STring:"+ob2.Var2);


     ob1 integer:88
     ob1 String:I'm Object1
     ob2 integer:88
     ob2 String:I'm Object2


2.Static Methods.

If you apply static keyword with any method, it is known as a static method.

A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.

Static Method
	 //Program to get cube of a given number by static method  
	 class Example2{  
	   static int cube(int x){  
		 return x*x*x;  
	   public static void main(String args[]){  
		 int result=Example2.cube(5);  


3.Static blocks of code.

Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory.

Static block
	 class Example3{
	   static int num;
	   static String mystring;
		  num = 97;
		  mystring = "Static keyword in Java";
	   public static void main(String args[])
		  System.out.println("Value of num="+num);
		  System.out.println("Value of mystring="+mystring);


     Value of num=97
     Value of mystring=Static Keyword in Java


4. Nested Class.

A Class can be made static only if it is a nested Class. The nested static class can be accessed without having an object of outer class.

Nested class
	 class Example4{
	   //Static class
	   static class MyStaticClass{
		   static String str="Inside Class MyStaticClass";
	   public static void main(String args[])
		   MyStaticClass.str="Inside Class Example4";
		   System.out.println("String stored in str is- "+ MyStaticClass.str);


      String stored in str is- Inside Class Example1


Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...