Static and This:

In this article, I will talk to you more about these two keywords and their application in Java. There are several keywords in Java that you cannot use to name variables or identifiers. Static and this are the 2 keywords that fall into this category. The static keyword is used in memory management discussion and its main relationship is with the class itself. The keyword this also has a large functional scope in the Java programming language. This keyword is used to refer to the current object and the upper object.
static and this in Java

How are static and this in Java?

We said that the static keyword is used in memory management and its main relationship is with the class itself. This keyword is used in the following cases:
• In the case of class variables or variables
• In the case of class method or method
• In the case of blocks
• In the case of indoor class

Static variable in Java

If, in Java, when generating a variable, the programmer uses the static keyword, this variable should be used for common features among all members of the same class. For example, the name of the company for the employees of that company or the name of the university for students. In addition, the static variable receives the protection only once when the class is being loaded, and in this regard, the necessary arrangements should be taken by the programmer. The advantage of using the static variable in Java is that it saves memory.
class Student{  
     int rollno;  
     String name;  
     String college="ITS";  
}  
The code above is an example of the use of the static variable in the Java language. Suppose we have a list of 500 students. If we are to enter the name of the university for each of them, the amount of memory consumed will increase if we use the static variable, only as much as one-time use.
Example:
Program of static variable  
  
class Student8{  
   int rollno;  
   String name;  
   static String college ="ITS";  
     
Student8(int r,String n){  
   rollno = r;  
   name = n;  
   }  
 void display (){System.out.println(rollno+" "+name+" "+college);}  
  
public static void main(String args [] ){  
 Student8 s1 = new Student8(111,"Karan");  
 Student8 s2 = new Student8(222,"Aryan");  
   
s1.display();  
 s2.display();  
 }  
}
Output:
Output:111 Karan ITS
       222 Aryan ITS

Comparison between static counter and non-static counter

As you can see, in the code below the counter variable is not defined statically. So in this code, each object has its own variable, which is different in all the examples that are generated from this class. If you look at the output of this code, you can see that this variable has been added only once per object.
Example:
class Counter{  
int count=0;//will get memory when instance is created  
  
Counter(){  
count++;  
System.out.println(count);  
}  
  
public static void main(String args [] ){  
  
Counter c1=new Counter();  
Counter c2=new Counter();  
Counter c3=new Counter();  
  
}  
}
Output:
Output:1
       1
       1
static and this in Java
In another example, this time we define the counter variable statically. As a result of this variable, it will be shared among all members of this class. In this example, if something is added to the values of this variable, the counter variable will change equally. In this example, pay attention to the code and its output.
Example:
class Counter2{  
static int count=0;//will get memory only once and retain its value  
  
Counter2(){  
count++;  
System.out.println(count);  
}  
  
public static void main(String args [] ){  
  
Counter2 c1=new Counter2();  
Counter2 c2=new Counter2();  
Counter2 c3=new Counter2();  
  
}  
}

Output:

Output:1
       2
       3

Static method in Java

If you use the static keyword when producing a method, it will lead to 3 events. The first thing is that this method is no longer an example made of class and belongs to the class. The second thing is that this method can be invoked without the need to build any examples of the class, and the third thing is that it has access to static variables and changing their values.
Example:
Program of changing the common property of all objects(static field).  
  
class Student9{  
     int rollno;  
     String name;  
     static String college = "ITS";  
       
static void change(){  
     college = "BBDIT";  
     }  
  
Student9(int r, String n){  
     rollno = r;  
     name = n;  
     }  
  
void display (){System.out.println(rollno+" "+name+" "+college);}  
  
public static void main(String args [] ){  
    Student9.change();  
  
Student9 s1 = new Student9 (111,"Karan");  
    Student9 s2 = new Student9 (222,"Aryan");  
    Student9 s3 = new Student9 (333,"Sonoo");  
  
s1.display();  
    s2.display();  
    s3.display();  
    }  
}
Output:
Output:111 Karan BBDIT
       222 Aryan BBDIT
       333 Sonoo BBDIT
Consider another example.
Example:
Program to get cube of a given number by 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);  
  }  
}
Output:
Output:125

What are the limitations of static method?

Static methods have 2 large limitations. The first limitation is that these methods do not have access to methods that are not static, so they cannot be called. The second limitation is that the keywords this and super cannot be used in these methods.
Example:
class A{  
 int a=40;//non static  
   
public static void main(String args [] ){  
  System.out.println(a);  
 }  
}
Output:
Output:Compile Time Error
Given this example, you may have wondered why we consider the main method as a static method. In response to this question, we have to say that the reason for this is that if we want to use this method, there is no need to build another object and consume more memory.

Static blocks in Java

Static blocks are used in Java to give a basic value to every single static member. The static block is implemented in Java before the main method is implemented and at the stage where the classes are loaded.
Example:
class A2{  
  static{System.out.println("static block is invoked");}  
  public static void main(String args [] ){  
   System.out.println("Hello main");  
  }  
}
Output:
Output:static block is invoked
       Hello main
Another question that may arise is whether it is possible to run a program without a main method. In response to this question, we should also say that yes, this possibility was done in JDKs that had version below version 1.7, the implementation of the program without the need for main method and by static blocks.
Example:
class A3{  
  static{  
  System.out.println("static block is invoked");  
  System.exit(0);  
  }  
}
Output:
Output:static block is invoked (if not JDK7)
Output this program in new versions:
Output:Error: Main method not found in class A3, please define the main method as:
public static void main(String [] args)

Keyword this in Java

This key has a lot of functional scope in Java. This keyword is used to refer to the current object and the upper object. If we want to headline the applications of this keyword, we can refer to the following:
• Refers to the sample variable in your class
• Implicit calling of the methods of a class
• Call the contact of a class constractor
• Use as an argument at the time of calling the method
• Use as an argument in a connector
• Return the produced sample from a class from the inside of the method

Refers to the sample variable generated in the class

If at the time of queries in parts such as the connector and … The names of the variables in the class are the same as the input arguments, the keyword this will be useful. This key word is used to refer to the variables in the class. The following example is an example of the use of this keyword.
Example:
class Student{  
int rollno;  
String name;  
float fee;  
Student(int rollno,String name,float fee){  
rollno=rollno;  
name=name;  
fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+fee);}  
}  
class TestThis1{  
public static void main(String args [] ){  
Student s1=new Student(111,"ankit",5000f);  
Student s2=new Student(112,"sumit",6000f);  
s1.display();  
s2.display();  
}}
Output:
0 null 0.0
0 null 0.0
As you can see, by running this code, a problem was created and produced an incorrect output. This problem has been corrected by the following code.
Example:
class Student{  
int rollno;  
String name;  
float fee;  
Student(int rollno,String name,float fee){  
this.rollno=rollno;  
this.name=name;  
this.fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+fee);}  
}  
  
class TestThis2{  
public static void main(String args [] ){  
Student s1=new Student(111,"ankit",5000f);  
Student s2=new Student(112,"sumit",6000f);  
s1.display();  
s2.display();  
}}
Output:
111 ankit 5000
112 sumit 6000
Of course, to avoid this problem, it is better to write the app in a way that does not cause these problems. In the example below, we’ve shown you how to write this code.
Example:
class Student{  
int rollno;  
String name;  
float fee;  
Student(int r,String n,float f){  
rollno=r;  
name=n;  
fee=f;  
}  
void display(){System.out.println(rollno+" "+name+" "+fee);}  
}  
  
class TestThis3{  
public static void main(String args [] ){  
Student s1=new Student(111,"ankit",5000f);  
Student s2=new Student(112,"sumit",6000f);  
s1.display();  
s2.display();  
}}
Output:
111 ankit 5000
112 sumit 6000
We recommend that you use meaningful words and names for your own variables and arguments, and use the keyword this if you really need it.

Call method in class

You can use the keyword this to call the methods of a class. If you do not use this keyword, the compiler will automatically add this keyword to your code.
Example:
class A{  
void m(){System.out.println("hello m");}  
void n(){  
System.out.println("hello n");  
m();//same as this.m()  
this.m();  
}  
}  
class TestThis4{  
public static void main(String args [] ){  
A a=new A();  
a.n();  
}}
Output:
hello n
hello m

Call class constractor

Suppose your class has a few different connectors and you’re going to use one of your connectors within another connector in order to save writing code. In this case, it is better to use the keyword this.
Example:
class A{  
A(){System.out.println("hello a");}  
A(int x){  
this();  
System.out.println(x);  
}  
}  
class TestThis5{  
public static void main(String args [] ){  
A a=new A(10);  
}}
Output:
hello a
10
In this example, as you can see, the called contact counteractor has several parameters.
Example:
class A{  
A(){  
this(5);  
System.out.println("hello a");  
}  
A(int x){  
System.out.println(x);  
}  
}  
class TestThis6{  
public static void main(String args [] ){  
A a=new A();  
}}
Output:
5
hello a
This is also used to form a communication chain between connectors. The reason for this is also to shorten the code and make it more beautiful.
Example:
class Student{  
int rollno;  
String name,course;  
float fee;  
Student(int rollno,String name,String course){  
this.rollno=rollno;  
this.name=name;  
this.course=course;  
}  
Student(int rollno,String name,String course,float fee){  
this(rollno,name,course);//reusing constructor  
this.fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}  
}  
class TestThis7{  
public static void main(String args [] ){  
Student s1=new Student(111,"ankit","java");  
Student s2=new Student(112,"sumit","java",6000f);  
s1.display();  
s2.display();  
}}
Output:
111 ankit java null
112 sumit java 6000
You should note that the keyword this is necessary to be written in the first line of the connector.
Example:
class Student{  
int rollno;  
String name,course;  
float fee;  
Student(int rollno,String name,String course){  
this.rollno=rollno;  
this.name=name;  
this.course=course;  
}  
Student(int rollno,String name,String course,float fee){  
this.fee=fee;  
this(rollno,name,course);//C.T.Error  
}  
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}  
}  
class TestThis8{  
public static void main(String args [] ){  
Student s1=new Student(111,"ankit","java");  
Student s2=new Student(112,"sumit","java",6000f);  
s1.display();  
s2.display();  
}}
Output:
Compile Time Error: Call to this must be first statement in constructor

Use the keyword this as an input argument in a method

In event handling discussion, the keyword this may be used as an input argument in a method.
Example:
class S2{  
  void m(S2 obj){  
  System.out.println("method is invoked");  
  }  
  void p(){  
  m(this);  
  }  
  public static void main(String args [] ){  
  S2 s1 = new S2();  
  s1.p();  
  }  
}
Output:
method is invoked
In fact, this keyword is used in the Event Handling discussion when it is necessary to reference for a class.

Use the keyword this when calling the connector

With this feature, you can use the keyword this as a parameter for the connector.
Example:
class B{  
  A4 obj;  
  B(A4 obj){  
    this.obj=obj;  
  }  
  void display(){  
    System.out.println(obj.data);//using data member of A4 class  
  }  
}  
  
class A4{  
  int data=10;  
  A4(){  
   B b=new B(this);  
   b.display();  
  }  
  public static void main(String args [] ){  
   A4 a=new A4();  
  }  
}
Output:
Output:10

Use the keyword this in order to restore the generated sample

If the recursive data type in a method is of the same class type, we can use the keyword this to restore this instance.
Syntax:
return_type method_name(){  
return this;  
}
Example:
class A{  
A getA(){  
return this;  
}  
void msg(){System.out.println("Hello java");}  
}  
class Test1{  
public static void main(String args [] ){  
new A().getA().msg();  
}  
}
Output:
Hello java
At the end of this article, we will test the keyword this to see if this keyword is able to return the same class instance or not? In the example below, we have done this test.
Example:
class A5{  
void m(){  
System.out.println(this);//prints same reference ID  
}  
public static void main(String args [] ){  
A5 obj=new A5();  
System.out.println(obj);//prints the reference ID  
obj.m();  
}  
}
Output:
A5@22b3ea59
A5@22b3ea59

And in the end,

Mirbazorgi’s website aims to help you learn and fix your problems by providing articles and practical experiences. Email me if you have any questions.