In this tutorial, we will discuss Usage of Static in Java programming language
In Java programming language, Static keyword(reserve word) is used to memory management mainly. But “static” use a lot of ways in Java. Static can be applied in Java’s:
We can declare any variable as static(with the static keyword) that is called the static variable.
The static variables can be used to refer to the common property of all object in class such as blocks, methods and class.
The syntax of static variable
static <data_type><variable_name>
Example
static int age; static string name;
we can use the static keyword in the class level variable(instance variable).
class student{ int age; int marks; //normal variable inside the class static string school_Name; //static variable inside the class }
Example
class Family_members{ int age; //instance variable declaration String f_Name; //non static variable inside the class static String l_Name="Kanapathy"; //static variable inside the class Family_members(int age_1,String name_1){ age=age_1; f_Name=name_1; } void display() { System.out.println(f_Name+" "+l_Name+" "+"age is"+" "+age); } public static void main(String args[]){ Family_members f1=new Family_members(24,"Kamalan"); Family_members f2=new Family_members(17,"Ramesh"); Family_members f3=new Family_members(20,"Nimalan"); f1.display(); f2.display(); f3.display(); } }
When the above code is executed, it produces the following results:
Kamalan Kanapaty age is 24 Ramesh Kanapaty age is 17 Nimalan Kanapaty age is 20
In this example, the static variable (static String l_Name=”Kanapathy”;) string 1_Name is shared to all the instance of the class
The static variable can be accessed without using an object of its class are created. So we can access directly
class ExampleStatic{ static int age;//static veriable static String name; static void display(){//static method System.out.println("age is :"+age); System.out.println("name is :"+name); } public static void main(String args[]){ display();//can be access derectly } }
When the above code is executed, it produces the following results:
age is :0 name is :null
In the above program, we are accessing static method display() without creating any object in this class
class ExampleNonStatic{ int age; //non static variable String name; static void display(){ System.out.println("age is "+age); System.out.println("name is "+name); } public static void main(String args[]){ display(); } }
When the above code is executed, it produces the following results:
Compile time error
The non-static variable can not be accessed without using any object of its class
class ExampleStatic1{ static int age=34; //non static variable static String name="suman"; public static void main(String args[]){ System.out.println("age is "+age); System.out.println("name is "+name); } }
When the above code is executed, it produces the following results:
age is 34 name is suman
program 2
class ExampleStatic1{ int age=34; //non static variable String name="suman"; public static void main(String args[]){ System.out.println("age is "+age); System.out.println("name is "+name); } }
When the above code is executed, it produces the following results:
Compile time error
In the above program, age and name are non-static variables. it cannot be accessed from the main method
When we apply static keyword with any method, it is called a static method. Any static member can be accessed before any object of its class is created. and without reference any object
The syntax of the static method
static <return_type><method_name>
Example
static void display() {} static string student_name() {}
Program 1
class Static_Method{ //this is main method inside the class public static void main(String args[]){ // here we can call the static method my_School(); } //this is static method(); static void my_School() { System.out.println("this is my school"); } }
When the above code is executed, it produces the following results:
this is my school
The static method (my_School())can be accessed before any object of its class is created. and without reference any object.
So we can directly access this static method(my_School())
Program 2
The main method is static, the static variable can be accessed directly from the main method
class Static_methodEx{ static int marks=56;//static veriable static String subject="Science"; public static void main(String args[]){ //main method as static System.out.println("Marks is :"+marks); System.out.println("Subject is :"+subject); } }
When the above code is executed, it produces the following results:
Marks is :56 Subject is :Science
Program 3
If the main method is static, static methods and non-static methods can be accessed directly from the main method
class Static_methodEx2{ static int marks=78;//static veriable static String subject="Mathematics"; static void getDet(){//static method System.out.println("Marks is :"+marks); System.out.println("Subject is :"+subject); } void display(){//non static method getDet();//static method called inside non static method } public static void main(String args[]){ //main method is also static method Static_methodEx2 obj=new Static_methodEx2(); //object create to call non static method obj.getDet(); } }
When the above code is executed, it produces the following results:
Marks is :78 Subject is :Mathematics
The syntax of the static block
static {}
Example
static { //some code here }
program 1
class My_Details{ static int my_Age; //static variable static String my_Name; static double my_Weight; static{ //static block my_Age=23; //initialized static variable my_Name="Robbert hook"; my_Weight=75.9; } public static void main(String args[]){ System.out.println("My age is :"+my_Age); System.out.println("My name is :"+my_Name); System.out.println("My age is :"+my_Weight); } }
When the above code is executed, it produces the following results:
My age is :23 My name is :Robbert Hook My weight is :75.9
A class can have more than one static block which is known as multiple static blocks. Now let’s see how to work multiple static blocks in Java
The syntax of the static block
static {} static {}
Example
static {//first static block //some code here } static { // second static block //some code here }
class My_Details{ static int age; //static variable static String name; static double weight; static{ //first static block age=23; //initialized static variable name="Robbert hook"; weight=75.9; } static{ //second static block age=21; //initialized static variable name="Jhon seela"; weight=100.9; } public static void main(String args[]){ System.out.println("Age is :"+age); System.out.println("Name is :"+name); System.out.println("Weight is :"+weight); } }
When the above code is executed, it produces the following results:
Age is :21 Name is :Jon seela Weight is:100.9
Static blocks are executed according to the given order. therefore first static block is overwritten by second static block
There are other Java language keywords that are similar to this keyword
float keyword in Java
Usage of this statement in the Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…