Thursday, 15 October 2015

Tutorial On This Keyword In Java

This keyword in java has many features but in this tutorial we are going to discussed specific feature of this keyword.In this tutorial we tried show how this keyword is used to refer current instance variable in this case this keyword is used when we want to use same name of both insatance varaible and local variable then we used this keyword with to distinguish between them.following example illustrates this keyword.

CODING:

class current
{
double current;
double resistance;
//Creating Parameterize constructor
current (double current,double resistance)
{
this.current = current;
this.resistance = resistance;
}

double volt() 
{
return current*resistance;
}
}

class voltage
{
public static void main(String args[])
{
//Creating Object Of current class
current cur=new current(10,40);
current cur1=new current(30,10);
System.out.println("Voltage is = "+cur.volt()+" volt");
System.out.println("Voltage is = "+cur1.volt()+" volt");
}
}  

OUTPUT:

voltage is=400volt
voltage is=300volt



1 comment: