keyword

Usage of Static in Java programming language

Usage of Static in Java programming language

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:

  • fields(variable) ,
  • methods
  • inner class and  nested class
  • blocks.

 

static variable

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.

Declaration

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

}

Usage of the static variable in Java

Usage 1- Static variables are shared among all the instances of 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

 

Usage 2- Static variables can access directly in static methods

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

 

Non Static variables cannot access directly in static methods
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

 

Usage 3- Static variables can access directly in the main method in Java

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

 

Non-static variables cannot be accessed directly in the main method in Java

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

 

static 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

Declaration

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

 

static block

Declaration

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

 

Multiple static blocks

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

Declaration

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

abstract keyword in Java

boolean Keyword in Java

break keyword in Java

Byte keyword in Java 

case keyword in Java 

catch keyword in Java 

char keyword in Java 

class keyword in Java 

continue keyword in Java 

default keyword in Java 

do keyword in Java

double keyword in Java

else keyword in Java

enum keyword in Java

extends keyword in Java

final keyword in Java

finally keyword in Java

float keyword in Java

for keyword in Java

if keyword in Java

static keyword in Java

super keyword in Java

 

Suggested for you

Usage of this statement in the Java language

Keywords in Java language

Keywords in C language

Keywords in Python language

 

 

The default keyword in Java programming language
The const keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago