// Use of this to refer the instance member in case of name conflict between local variable and //instance variable
class ThisDemo
{
int a;
int b;
ThisDemo1(int a,int b)
{
//a=a;
this.a = a;
// b=b;
this.b = b;
}
public static void main(String [] rks)
{
ThisDemo1 td = new ThisDemo1(50,30);
System.out.println(td.a);
System.out.println(td.b);
}
}
// Use of this for constructor Chaining
class A
{
public static void main(String ravi[])
{
A a = new A(1);
}
A()
{
//this(1);
System.out.println("Default Constructor");
}
A(int x)
{
this(1, 1.5);
System.out.println("One argumented Constructor");
}
A(int x, double d)
{
this('c');
System.out.println("Two Argumented Constructor");
}
A(char z)
{
this();
System.out.println("Char Constructor");
}
}
class ThisDemo
{
int a;
int b;
ThisDemo1(int a,int b)
{
//a=a;
this.a = a;
// b=b;
this.b = b;
}
public static void main(String [] rks)
{
ThisDemo1 td = new ThisDemo1(50,30);
System.out.println(td.a);
System.out.println(td.b);
}
}
// Use of this for constructor Chaining
class A
{
public static void main(String ravi[])
{
A a = new A(1);
}
A()
{
//this(1);
System.out.println("Default Constructor");
}
A(int x)
{
this(1, 1.5);
System.out.println("One argumented Constructor");
}
A(int x, double d)
{
this('c');
System.out.println("Two Argumented Constructor");
}
A(char z)
{
this();
System.out.println("Char Constructor");
}
}
Good Post! Thank you so much for sharing the nice post, it was so good to read and useful to improve
ReplyDeleteJava Training in Electronic City