Java Static keyword (Class, Block, Methods and Variables )

Java Static keyword (Class, Block, Methods and Variables )
Java Static keyword (Class, Block, Methods and Variables ) 1

The static keyword in java is used to memory management, static is the keyword which is the common property to all objects. Static keyword can be applied for Class, Block, Methods and Variables

Java static variable

Static variable is used as a class wise variable,it is a common property for all objects ,all objects(instances) share the same variable so it is called static variable or class variable

Static variable memory is allocated only once,while the class is being used

Syntax

static data_type variable_name;

static variable can be declared by using static keyword as a prefix for a normal variable deceleration

example

 class Student{
    int age;  
    static String course="CSE";
    Student(int a)
    {
        age=a;
    }
    
    

    public static void main(String args[])
    {
        Student s1=new Student(23);
        Student s2=new Student(14);
        System.out.println("age of the student is "+s1.age+" course"+"="+course);
        System.out.println("age of the student is "+s2.age+" course"+"="+course);
        //changing course for all students
        Student.course="EEE";
        System.out.println("age of the student is "+s2.age+" course"+"="+course);
    }
}
age of the student is 23 course=CSE
age of the student is 14 course=CSE
age of the student is 14 course=EEE

In the example given above ,the program is about the students of the same course ,so the course variable should be unique for all the objects or students of the class. so course=CSE is declared as a static variable

we can also change the value of static variable in the program by using dot operator

Student.course="EEE";

so the 3rd statement in the output has the course value as EEE

Java static method

static method is similar to static variable,static method can access only static data ,this static method belongs to its class ,this can be invoked even without creating the instance of the object

Syntax

static return_type fun_name()

static method can be declared by using static keyword as a prefix for a normal function deceleration

example

 class Student{
    int age;  
    static String course="CSE";
    static void changecourse(){  
        course = "EEE";  
        }  
    Student(int a)
    {
        age=a;
    }
    
    

    public static void main(String args[])
    {
        Student s1=new Student(23);
        Student s2=new Student(14);
        System.out.println("age of the student is "+s1.age+" course"+"="+course);
        System.out.println("age of the student is "+s2.age+" course"+"="+course);
        //changing course for all students
        changecourse();
        System.out.println("age of the student is "+s2.age+" course"+"="+course);
    }
}
age of the student is 23 course=CSE
age of the student is 14 course=CSE
age of the student is 14 course=EEE

In the example given above ,the program is about the students of the same course ,so the course variable should be unique for all the objects or students of the class. so course=CSE is declared as a static variable

now to change the course value for all students we used a static function called coursechange to change the value of course variable,we have accessed the function without using any of the objects

changecourse();

we just direct used a function inside main class because it is a static function, static method cannot be used with the non-static data members and in the main java function non-static methods cannot be called directly

Java static block

Java static block is a collection of static statements which are executed when class is loaded into memory by Java class loader. Static block is only executed only once when class is loaded

java static block can contain static elements like static members or variables,static functions

There can be multiple static blocks in a class static, but there is no difference because static blocks are executed only once

Syntax

static {
   //code
}

static block can be created by using static keyword and its body should be written in { }

Example

class block{  
  static{System.out.println("static block is invoked");
}  
  public static void main(String args[]){  
   System.out.println("main is invoked");  
  }  
}  
static block is invoked
main is invoked

check the output of the above program,first the static block will be invoked and next main will be invoked

Java Static Class

Default image
Vishal Devxo
Vishal is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python
Leave a Reply