Programs Using "this" Keyword

"this" Keyword.

"this" keyword is used to specify the current object. It can be used at 3 levels:
  1. Variable Level.
  2. Constructor Level.
  3. Method Level.
Note:

At any level "this" must be the first statement of a block or scope of the level.

Programs Using "this" at Variable Level.

when local and instance variables are declared as same then we can use "this" keyword to make difference between them , otherwise the Compiler cant recognize the difference between them and gives a compilation error .

class ThisVariable
{
String name="Void Main Technologies";

void print(String name)
{
System.out.println("instance values before assigning to local variable:"+this.name+"\n");

this.name=name;

System.out.println("instance values after assigning to local variable:"+this.name);
}

public static void main(String[] args) 
{
ThisVariable tv=new ThisVariable();

String name="Institute for all technical courses";

tv.print(name);

}

}

Save:

ThisVariable.java

Compile:

javac ThisVariable.java

Run:

java ThisVariable

Output:

instance values before assigning to local variable:void Main Technologies
instance values after assigning to local variable:Institute for all technical courses.


Programs Using "this" at Constructor Level.


class ThisConstructor

{

String name;



String caption;



ThisConstructor(String name,String caption)

{

this.name=name;

this.caption=caption;



System.out.println("\t"+name);
System.out.println("*****"+caption+"*****");

}

public static void main(String[] args) 
{
String localName="Void Main Technologies";

String localCaption="Institute for all technical courses";

ThisConstructor tc=new ThisConstructor(localName,localCaption);
}
}

Save:

ThisConstructor.java

Compile:

javac ThisConstructor.java

Run:

java ThisConstructor

Output:
                 
                   void Main Technologies
*****Institute for all technical courses*****

Programs Using "this" at Method Level.

class ThisMethod
{
String name;

String caption;

void thisMethod(String name,String caption)
{
this.name=name;
this.caption=caption;

System.out.println("\t"+name);
System.out.println("*****"+caption+"*****");
}

public static void main(String[] args) 
{
String localName="Void Main Technologies";

String localCaption="Institute for all technical courses";

ThisMethod tc=new ThisMethod();
tc.thisMethod(localName,localCaption);
}
}

Save:

ThisMethod.java

Compile:

javac ThisMethod.java

Run:

java ThisMethod

Output:
                 
                   void Main Technologies
*****Institute for all technical courses*****


Programs Using "this" at Constructor Call.


class ThisConstructorCall

{

String instituteName;



String location;



ThisConstructorCall()

{

System.out.println("Details of Institute\n");
}

ThisConstructorCall(String instituteName,String location)
{
this();

this.instituteName=instituteName;
this.location=location;

System.out.println("NAME OF THE INSTITUTE:\n"+instituteName+"\n\n"+"LOCATION OF INSTITUTE:\n"+location);
}

public static void main(String[] args) 
{
String name="Void Main Technologies";

String loc="Dilsuknagar";

ThisConstructorCall tcc=new ThisConstructorCall(name,loc);
}
}

Save:

ThisConstructorCall.java

Compile:

javac ThisConstructorCall.java

Run:

java ThisConstructorCall

Output:

Details of Institute

NAME OF THE INSTITUTE:   void Main Technologies
LOCATION OF INSTITUTE:    Dilsuknagar.

Note :

Even the parameterized constructor can also be called by using "this" Keyword. Number of parameters must be same as the number of parameters in the constructor.

Programs Using "this" at Method Call.


class ThisMethodCall

{

String instituteName;



String location;



void thisMethodCall()
{
System.out.println("Details of Institute\n");
}

void MethodCall(String instituteName,String location)
{
this.thisMethodCall();

this.instituteName=instituteName;
this.location=location;

System.out.println("NAME OF THE INSTITUTE:\n"+instituteName+"\n\n"+"LOCATION OF INSTITUTE:\n"+location);
}

public static void main(String[] args) 
{
String name="Void Main Technologies";

String loc="Dilsuknagar";

ThisMethodCall tmc=new ThisMethodCall();
tmc.MethodCall(name,loc);
}
}


Save:



ThisMethodCall.java



Compile:



javac ThisMethodCall.java

Run:

java ThisMethodCall

Output:

Details of Institute

NAME OF THE INSTITUTE:  void Main Technologies
LOCATION OF INSTITUTE:    Dilsuknagar.

Note:

Here methods can be called by using "this.methodName()" , otherwise if method name is not specified the it may refers to the constructor . 





Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java

Java Inner Classes