Java This Keyword

  1. Identify local and Global variables using this keyword in Java
  2. Get Address of the Object using this keyword in Java
  3. Area of Rectangle using this keyword in Java
  4. Access Multi methods within a method using this in Java
  5. Use this keyword with constructors in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Identify local and Global variables using this keyword in Java


This_Ex1.java

public class This_Ex1 {
 
	int a = 10;
	
	void show() {
		
		int a = 20;
				
		System.out.println("The value of (Local Variable) 'a' is : " + a);
		System.out.println("The value of (Global Variable) 'a' is : " + this.a);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		This_Ex1 obj = new This_Ex1();
		
		obj.show();
	}
}

Sample Output

The value of (Local Variable) 'a' is : 20
The value of (Global Variable) 'a' is : 10




B. Get Address of the Object using this keyword in Java


This_Ex2.java

public class This_Ex2 {
	
	void show() {
		
		System.out.println("It shows the Address of the Object \n" +
				"which one is called this operation : " + this + "\n");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		This_Ex2 obj1 = new This_Ex2();
		This_Ex2 obj2 = new This_Ex2();
		This_Ex2 obj3 = new This_Ex2();
		
		obj1.show();
		obj2.show();
		obj3.show();
	}
}

Sample Output

It shows the Address of the Object 
which one is called this operation : This_Ex2@9931f5

It shows the Address of the Object 
which one is called this operation : This_Ex2@19ee1ac

It shows the Address of the Object 
which one is called this operation : This_Ex2@1f1fba0




C. Area of Rectangle using this keyword in Java


This_Ex3.java

public class This_Ex3 {
	
	int length, breadth;
	
	void area(int length, int breadth) {
		
		this.length = length;
		this.breadth = breadth;
		
		int area = this.length * this.breadth;
		
		System.out.println("The Area of the Rectangle is : " + area);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		This_Ex3 obj = new This_Ex3();
		
		obj.area(6, 4);
	}
}

Sample Output

The Area of the Rectangle is : 24




D. Access Multi methods within a method using this in Java


This_Ex4.java

public class This_Ex4 {
	
	int id;
	float mark;
	double height;
	boolean pass;
	String name;
	
	This_Ex4(int id, float mark, 
			double height, boolean pass, String name) {
		
		this.id = id;
		this.mark = mark;
		this.height = height;
		this.pass = pass;
		this.name = name;
	}
	
	int getId() {
				
		return this.id;
	}
	
	float getMark() {
		
		return this.mark;
	}
	
	double getHeight() {
		
		return this.height;
	}
	
	boolean getPass() {
		
		return this.pass;
	}
	
	String getName() {
		
		return this.name;
	}
	
	void display() {
		
		System.out.println("The Student Details");
		
		System.out.println("\nID : " + this.getId());
		System.out.println("Mark : " + this.getMark());
		System.out.println("Height : " + this.getHeight());
		System.out.println("Pass : " + this.getPass());
		System.out.println("Name : " + this.getName());
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		This_Ex4 obj = new This_Ex4(11, (float) 95.65, 172.6, true, "Siva");
		
		obj.display();
	}
}

Sample Output

The Student Details

ID : 11
Mark : 95.65
Height : 172.6
Pass : true
Name : Siva




E. Use this keyword with constructors in Java


This_Ex5.java

public class This_Ex5 {
	
	int a = 0;
	int b = 0;
	
	This_Ex5(int x, int y) {
		
		this.a = x;
		this.b = y;
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		This_Ex5 obj1 = new This_Ex5(11,18);
		This_Ex5 obj2 = new This_Ex5(111,118);
		
		System.out.println(obj1.a);
		System.out.println(obj1.b);
		System.out.println(obj2.a);
		System.out.println(obj2.b);
	}
}

Sample Output

11
18
111
118






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com