0% found this document useful (0 votes)
4 views

06-Final, static and this Keyword In Java

The document explains the use of the 'final' and 'static' keywords in Java, detailing their applications with variables, methods, and classes. It highlights that 'final' variables cannot be changed once initialized, while 'static' members belong to the class rather than instances and are used for memory management. Additionally, the document discusses the 'this' keyword, which refers to the current object and resolves ambiguity between instance variables and parameters.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

06-Final, static and this Keyword In Java

The document explains the use of the 'final' and 'static' keywords in Java, detailing their applications with variables, methods, and classes. It highlights that 'final' variables cannot be changed once initialized, while 'static' members belong to the class rather than instances and are used for memory management. Additionally, the document discusses the 'this' keyword, which refers to the current object and resolves ambiguity between instance variables and parameters.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Final Keyword In Java

The final keyword in java is used to restrict the user. The java final keyword can be used in
many context. Final can be:

1. variable
2. method
3. class

The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be initialized in the static block only.
We will have detailed learning of these. Let's first learn the basics of final keyword.

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be
constant).

Example of final variable

class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
Output:Compile Time Error

Java static keyword


The static keyword in java is used for memory management mainly. We can apply java static
keyword with variables, methods, blocks and nested class. The static keyword belongs to the
class than instance of the class.

The static can be:

1. variable (also known as class variable)


2. method (also known as class method)
3. block
4. nested class

1) Java static variable

If you declare any variable as static, it is known static variable.

 The static variable can be used to refer the common property of all objects (that is not unique
for each object) e.g. company name of employees,college name of students etc.
 The static variable gets memory only once in class area at the time of class loading.

class Student{
int rollno;
String name;
static String college ="SIT";

Student(int r,String n){


rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}

public static void main(String args[]){


Student s1 = new Student(111,"ABC");
Student s2 = new Student(222,"XYZ");

s1.display();
s2.display();
}
}
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:1
2
3

2) Java static method

If you apply static keyword with any method, it is known as static method.

 A static method belongs to the class rather than object of a class.


 A static method can be invoked without the need for creating an instance of a class.
 static method can access static data member and can change the value of it.
class Student{
int rollno;
String name;
static String college = "SIT";

static void change(){


college = "BBDIT";
}

Student(int r, String n){


rollno = r;
name = n;
}

void display (){System.out.println(rollno+" "+name+" "+college);}

public static void main(String args[]){


Student.change();

Student s1 = new Student (111,"ABC");


Student s2 = new Student (222,"PQR");
Student s3 = new Student (333,"XYZ");

s1.display();
s2.display();
s3.display();
}
}

111 ABC BBDIT


222 PQR BBDIT
333 XYZ BBDIT

Restrictions for static method

There are two main restrictions for the static method. They are:
1. The static method cannot use non static data member or call non-static method directly.
2. 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);
}
}

Output:Compile Time Error

Can we execute a program without main() method?

Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.
class A3{
static{
System.out.println("static block is invoked");
System.exit(0);
}
}

Output:static block is invoked (if not JDK7)

In JDK7 and above, output will be:

Output:Error: Main method not found in class A3, please define the main method
as:
public static void main(String[] args)

this keyword in java


In java, this is a reference variable that refers to the current object.

Usage of java this keyword

Here is given the 6 usage of java this keyword.

1. this keyword can be used to refer current class instance variable.


2. this() can be used to invoke current class constructor.
3. this keyword can be used to invoke current class method (implicitly)
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this keyword can also be used to return the current class instance.

1) The this keyword can be used to refer current class instance variable.

If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of
ambiguity.

class Student{
int id;
String name;
Student(int id,String name)
{
id = id;
name = name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student s1 = new Student(111,"ABC");
Student s2 = new Student(321,"XYZ");
s1.display();
s2.display();
}
}

Output:0 null
0 null
In the above example, parameter (formal arguments) and instance variables are same

class Student{
int id;
String name;

Student(int id,String name){


this.id = id;
this.name = name;
}
void display()
{
System.out.println(id+" "+name);
}

public static void main(String args[]){


Student s1 = new Student(111,"ABC");
Student s2 = new Student(321,"XYZ");
s1.display();
s2.display();
}
}
Output:111 ABC
222 XYZ

2) this() can be used to invoked current class constructor.

this ();//it is used to invoked current class constructor.

3)The this keyword can be used to invoke current class method (implicitly).

You may invoke the method of the current class by using the this keyword. If you don't use the
this keyword, compiler automatically adds this keyword while invoking the method. Let's see the
example
class S{
void m(){
System.out.println("method is invoked");
}
void n(){
this.m();//no need because compiler does it for you.
}
void p(){
n();//complier will add this to invoke n() method as this.n()
}
public static void main(String args[]){
S s1 = new S();
s1.p();
}
}

4) this keyword can be passed as an argument in the 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();
}
}
Output:method is invoked
5) The this keyword can be passed as argument in the constructor call.

6) The this keyword can be used to return current class instance.

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();
}
}

You might also like