Keyword for Variable Hiding
class Jbt
{
int variable = 5;
public static void
main(String args[])
{
Jbt obj = new Jbt();
obj.method(20);
obj.method();
}
void method(int
variable)
{
variable = 10;
System.out.println("Value
of Instance variable :" + this.variable);
System.out.println("Value
of Local variable :" + variable);
}
void method()
{
int variable = 40;
System.out.println("Value
of Instance variable :" + this.variable);
System.out.println("Value
of Local variable :" + variable);
}
}
This with Constructor
class Jbt
{
Jbt()
{
this("Jbt");
System.out.println("Inside
Constructor without parameter");
}
Jbt(String str)
{
System.out.println("Inside
Constructor with String parameter as " + str);
}
public static void
main(String[] args)
{
Jbt obj = new Jbt();
}
}
This keyword with Method
class Jbt
{
public static void
main(String[] args)
{
Jbt obj = new Jbt();
obj.methodTwo();
}
void methodOne()
{
System.out.println("Inside
Method One");
}
void methodTwo()
{
System.out.println("Inside
Method Two");
this.methodOne();
}
}
This keyword as method parameter
public class JbtThisAsParameter
{
public static void
main(String[] args)
{
Jbt1 obj = new Jbt1();
obj.i = 10;
obj.method();
}
}
class Jbt1 extends JbtThisAsParameter
{
int i;
void method()
{
method1(this);
}
void method1(Jbt1 t)
{
System.out.println(t.i);
}
}
No comments:
Post a Comment