Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
this Keyword in Java
Previous Home Next

this is a keyword in Java. Which can be used inside method or constructor of class. "this" is a reference variabe which stores the reference of the method calling object. Every class contains only one "this" keyword irrespective of number of objects. When we call a member method of a class through an object then the reference of the calling object is automatically passsed an argument to the method and this reference will be received by the "this" inside the class. Also the name of every class member variable will be prefixed by this and dot operator automatically this is called as implict use of "this" keyword.

Sometimes the name of local variables is same as of instance member variable the priority will be given to local variable. It means local variable will override instance member variable. This is called as instance variable hiding. To use member variable in this case we have to prefix "this" and the dot operator before member name. this is called as explicit use of "this" keyword.

this can be passed as an argument to another method.

Instance variable Hiding

In java it is illegal to declare two local variables with the same name inside the same or enclosing scopes. But we can have local variables (parameters of member methods) having same name as of instance member variable of class. When a local variable has the same name as an instance variable, the local variables hides the instance variable. But this keyword allows us to refer to instance variable even if local variables hides it.

Syntax of using this keyword

this.varName

varName is a name of an instance variable.

Another use of the this keyword is it allows one constructor to explicitly call another constructor in the same class.

Example :

class C
{
	private int x;
	private int y;
	void setData(int x,int y)
	{
		this.x=x; //this.x is the class member & x is local variable
		this.y=y;
	}
	void display()
	{
		System.out.println(x+"\t"+y);
	}
}

class CTest
{
	public static void main(String args[])
	{
		C a1=new C();
		C a2=new C();
		a1.setData(10,20);
		a2.setData(5,7);
		a1.display();
		a2.display();
	}
}

output :

10    20
5      7
Previous Home Next