Static Variable and Method
Static Variable and Method
3. Block
4. Nested class
o 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.
o The static variable gets memory only once in the class area at the time of
class loading.
2. iit rollno;
3. String name;
4. String college="ITS";
5. }
Suppose there are 500 students in my college, now all instance data
members will get memory each time when the object is created. All students
have its unique rollno 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 feld will get the memory only once.
class Student{
String name;
//constructor
rollno = r;
name = n;
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
Output:
//which get memory each time when we create an object of the class.
class Counter{
iit count=0;//will get memory each time when the instance is created
Counter(){
count++;//incrementing value
System.out.println(count);
//Creating objects
Output:
1
1
1
class Counter2{
static iit count=0;//will get memory only once and retain its value
Counter2(){
System.out.println(count);
//creating objects
Output:
1
2
3
2) Java static method
If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o A static method can be invoked without the need for creating an instance of a
class.
o A static method can access static data member and can change the value of
it.
class Student{
iit rollno;
String name;
college = "BBDIT";
rollno = r;
name = n;
//creating objects
s1.display();
s2.display();
s3.display();
}
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
class Calculate{
returi x*x*x;
iit result=Calculate.cube(5);
System.out.println(result);
}
Output:125
Restrictions for the static method
There are two main restrictions for the static method. They are:
1. The static method can not use non static data member or call non-static
method directly.
class A{
System.out.println(a);
}
Output:Compile Time Error
System.out.println("Hello main");
}
Output:static block is invoked
Hello main
Q) Can we execute a program without main() method?
Ans) No, one of the ways was the static block, but it was possible till JDK 1.6.
Since JDK 1.7, it is not possible to execute a Java class without the main
method.
class A3{
static{
System.exit(0);
Output:
Error: Main method not found in class A3, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx
6. this can be used to return the current class instance from the method.
class Student{
iit rollno;
String name;
float fee;
rollno=rollno;
name=name;
fee=fee;
class TestThis1{
public static void main(String args[]){
s1.display();
s2.display();
}}
Output:
0 null 0.0
0 null 0.0
iit rollno;
String name;
float fee;
this.rollno=rollno;
this.name=name;
this.fee=fee;
class TestThis2{
s2.display();
}}
Output:
iit rollno;
String name;
float fee;
rollno=r;
name=n;
fee=f;
class TestThis3{
s1.display();
s2.display();
}}
Output:
111 ankit 5000
112 sumit 6000
It is better approach to use meaningful names for variables. So we use same name
for instance variables and parameters in real time, and always use this keyword.
class A{
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
class TestThis4{
A a=iew A();
a.n();
}}
Output:
hello n
hello m
class A{
A(){System.out.println("hello a");}
A(iit x){
this();
System.out.println(x);
class TestThis5{
A a=iew A(10);
}}
Output:
hello a
10
class A{
A(){
this(5);
System.out.println("hello a");
A(iit x){
System.out.println(x);
class TestThis6{
A a=iew A();
}}
Output:
5
hello a
class Student{
iit rollno;
String name,course;
float fee;
this.rollno=rollno;
this.name=name;
this.course=course;
this(rollno,name,course);//reusing constructor
this.fee=fee;
}
class TestThis7{
s1.display();
s2.display();
}}
Output:
class Student{
iit rollno;
String name,course;
float fee;
this.rollno=rollno;
this.name=name;
this.course=course;
this.fee=fee;
this(rollno,name,course);//C.T.Error
class TestThis8{
s1.display();
s2.display();
}}
Compile Time Error: Call to this must be first statement in constructor
class S2{
System.out.println("method is invoked");
void p(){
m(this);
S2 s1 = iew S2();
s1.p();
Output:
method is invoked
Application of this that can be passed as an argument:
class B{
A4 obj;
B(A4 obj){
this.obj=obj;
void display(){
class A4{
iit data=10;
A4(){
B b=iew B(this);
b.display();
A4 a=iew A4();
}
Output:10
returi this;
}
Example of this keyword that you returi as a
statemeit from the method
class A{
A getA(){
returi this;
class Test1{
iew A().getA().msg(); }
Output:
Hello java