What you read in this article:
Static and This:
My name is Arsalan Mirbozorgi and in this article, I am trying to talk to you about two keywords in Java. There are so many different types of keywords in Java that you cannot use them to name variables or identify them. Static and this are two keywords that are among the keywords that you cannot use. Static is used in the discussion of memory management and is mostly related to the class itself. This domain has many uses in the Java programming language. This keyword is used to refer to the current object and the object above.
How are static and this in Java?
We have already told you that statics is used in the field of memory management and its communication is more with the class itself. This keyword is used in various cases. These include:
- Variable or class variable
- Method or class method
- Block
- Internal class
Static variable in Java
If a programmer uses a static keyword when generating a variable in the Java programming language, a variable for common attributes must be used for all members of a class. For example, the name of the company for the employees of a company or the name of the university for students. In addition, the static variable only receives memory once when the class is loading, so the programmer must make all the necessary arrangements. Another advantage of using the stand variable is that it saves a lot of memory.
class Student{ int rollno; String name; String college="ITS"; }
The code above is an example of using a static variable. Imagine that you have a list of five hundred students, and if you were to enter the names of universities for each of these students, memory usage would increase dramatically. However, if you use the static variable in this field, the memory university name will be used only once.
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:111 Karan ITS 222 Aryan ITS
Comparison between static counter and non-static counter
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:1 1 1
If we want to give you another example, it is better to define the counter variable statically this time. If we do this, the variable will be shared between all members of this class. In this example, if we add something to a variable, the counter variable changes as well. In this example, look at the input and output code.
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 a static keyword when generating a method, three things will happen to you. The first is that this method is no longer a constructed instance of the class but belongs to the class. The second case is that this method can be called in a situation where no instance of the class needs to be created. And the third thing is that this method can access static variables and change their values.
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:111 Karan BBDIT 222 Aryan BBDIT 333 Sonoo BBDIT
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:125
What are the limitations of static method?
In general, the static method has two very large limitations. The first limitation is that these methods have no access to non-static methods and cannot call them. The second limitation is that the keywords “this” and “super” cannot be used in these methods.
class A{ int a=40;//non static public static void main(String args [] ){ System.out.println(a); } }
Output:Compile Time Error
Static blocks in Java
class A2{ static{System.out.println("static block is invoked");} public static void main(String args [] ){ System.out.println("Hello main"); } }
Output:static block is invoked Hello main
Another question you may ask us is whether it is possible to run a program without a main method? To answer this question, we must say yes. This is possible on JDKs with a lower version than version 1.7, and you can run the program without the need for the main method and do so using static blocks.
class A3{ static{ System.out.println("static block is invoked"); System.exit(0); } }
Output:static block is invoked (if not JDK7)
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 keyword has many uses in the Java programming language. As we told you earlier, this keyword is used to refer to the current object and the object above. If we want to explain the uses of this keyword to you in the form of headlines and summaries, we can mention the following.
- Refers to the sample variable in a class
- Calling methods of a class implicitly
- Calling class constructor
- Use argument when calling a method
- Use as an argument when calling a method
- Returns the sample generated from a class from within the method
Refers to the sample variable generated in the class
If the names of the variables in the class are equal to the names of the input arguments at the time of assignment in different parts such as the constructor and so on, then using this keyword will be useful. In this case, the keyword is used to refer to variables in a class.
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(); }}
0 null 0.0 0 null 0.0
As you can see, this created a problem and produced incorrect output. This problem is corrected by the following code.
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(); }}
111 ankit 5000 112 sumit 6000
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(); }}
111 ankit 5000 112 sumit 6000
Call method in class
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(); }}
hello n hello m
Call class constractor
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); }}
hello a 10
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(); }}
5 hello a
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(); }}
111 ankit java null 112 sumit java 6000
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(); }}
Compile Time Error: Call to this must be first statement in constructor
Use the keyword this as an input argument in a method
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(); } }
method is invoked
Use the keyword this when calling the connector
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:10
Use the keyword this in order to restore the generated sample
return_type method_name(){ return this; }
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(); } }
Hello java
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(); } }
A5@22b3ea59 A5@22b3ea59
And in the end,
Mirbozorgi website will try to help you learn the concepts by providing articles and practical experiences. Email me if you have any questions.