Wednesday, June 3, 2015
this keyword
public class ThisKeyordDemo {
public static void main(String[] args) {
ClassName cNam = new ClassName();
cNam.methodName();
System.out.println("Reference className is "+cNam);
System.out.println("\n------------Another Object----------");
ClassName cNam2 = new ClassName();
cNam2.methodName();
System.out.println("Reference className is "+cNam2);
}
}
class ClassName{
int num = 10 ;
public void methodName(){
int num = 15;
System.out.println("This will print local variable num: "+num);
System.out.println("This will print the instance variable num "+this.num);
System.out.println("\nReference className is "+this);
}
}
-------------------------------------------------------------------------------------------------------------------------
public class ThisConstructors {
public static void main(String[] args) {
ClasName cName = new ClasName();
ClasName cName2 = new ClasName(1);
ClasName cName3 = new ClasName(2,6);
ClasName cName4 = new ClasName(3,7,true);
ClasName cName5 = new ClasName(4,8,true);
System.out.println("First obj "+cName);
System.out.println("Second obj "+cName2);
System.out.println("Third obj "+cName3);
System.out.println("Fourth obj "+cName4);
System.out.println("Fifth obj "+cName5);
}
}
class ClasName{
int x,y;
boolean b;
char c;
public ClasName() {
this(0, 0, false, ' ' );
}
public ClasName(int x) {
this(x, 0, false, ' ');
}
public ClasName(int x , int y) {
this(x, y, false, ' ');
}
public ClasName(int x , int y ,boolean b) {
this(x, y, b, ' ');
}
public ClasName(int x , int y , boolean b , char c) {
this.x = x;
this.y = y;
this.b = b;
this.c = c;
}
}
Subscribe to:
Post Comments (Atom)



No comments:
Post a Comment