Thursday, 30 April 2020

JAVA STATIC KEYWORD

JAVA STATIC KEYWORD

In Java, the static keyword is used for memory management. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

Java static variable:

If you declare any variable as static, it is known as a static variable.
The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time of class loading.

Advantages of static variable

It makes your program memory efficient (i.e., it saves memory).

Understanding the problem without static variable

class Mca{ 
     int roll_no; 
     String name; 
     String college="ITS"; 

Suppose there are 500 students in my class, now all instance data members will get memory each time when the object is created. All students have its unique roll_no and name, so instance data member is good in such case. Here, "college" refers to the common property of all objects. If we make it static, this field will get the memory only once.

Static Methods:

If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than the 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.

Example of static method


//Java Program to demonstrate the use of a static method. 

class Student{ 
     int rollno; 
     String name; 
     static String college = "ITS"; 
     //static method to change the value of static variable 
     static void change(){ 
     college = "BBDIT"; 
     } 
     //constructor to initialize the variable 
     Student(int r, String n){ 
     rollno = r; 
     name = n; 
     } 
     //method to display values 
     void display(){System.out.println(rollno+" "+name+" "+college);} 
//Test class to create and display the values of object 
public class TestStaticMethod { 
    public static void main(String args[]){ 
    Student.change();//calling change method 
    //creating objects 
    Student s1 = new Student(111,"Karan"); 
    Student s2 = new Student(222,"Aryan"); 
    Student s3 = new Student(333,"Sonoo"); 
    //calling display method 
    s1.display(); 
    s2.display(); 
    s3.display(); 
    } 

Output:111 Karan BBDIT
       222 Aryan BBDIT
       333 Sonoo BBDIT


Another example of a static method that performs a normal calculation

//Java Program to get the cube of a given number using the static method 
 
class Calculate{ 
  static int cube(int x){ 
  return x*x*x; 
  } 
 
  public static void main(String args[]){ 
  int result=Calculate.cube(5); 
  System.out.println(result); 
  } 

Restrictions for the static method

There are two main restrictions for the static method. They are:

The static method can’t use non static data member or call non-static method directly.
this and super cannot be used in static context.
class A{ 
 int a=40;//non static 
    public static void main(String args[]){ 
  System.out.println(a); 
 } 
}    

Important Points:

When to use static methods?

When you have code that can be shared across all instances of the same class, put that portion of code into static method.
They are basically used to access static field(s) of the class.

Instance method vs Static method

Instance method can access the instance methods and instance variables directly.
Instance method can access static variables and static methods directly.
Static methods can access the static variables and static methods directly.
Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use this keyword as there is no instance for ‘this’ to refer to.

Why is the Java main method static?

It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...