THIS keyword for variable hiding and in method

package this_keyword;
public class This_Keyword {
int c = 100;
void A() {
int c = 50;
System.out.println("accessing global variable" + this.c);
System.out.println("accessing local variable " + c);
}
void B(int c) // paramerised constructor
{
c = 25;
System.out.println("accessing global variable" + this.c);
System.out.println("accessing local variable " + c);
}
public static void main(String[] args) {
This_Keyword ob = new This_Keyword();
ob.A();
System.out.println("FOR PARAMETRISED CONSTRUCTOR");
ob.B(15);
}
}

                            OUTPUT 
accessing global variable100
accessing local variable 50
FOR PARAMETRISED CONSTRUCTOR
accessing global variable100
accessing local variable 25
***********************************************************************
package this_keyword;
public class This_Keyword {
int c = 100;
void A() {
int c = 50;
System.out.println("accessing global variable at A " + this.c);
System.out.println("accessing local variable at A " + c);
}
void B(int c) // paramerised constructor
{
this.A();
c = 25;
System.out.println("accessing global variable at B " + this.c);
System.out.println("accessing local variable at B " + c);
}
public static void main(String[] args) {
This_Keyword ob = new This_Keyword();
ob.B(15);
}
}



OUTPUT

accessing global variable at A 100
accessing local variable at A 50
accessing global variable at B 100
accessing local variable at B 25











No comments:

Post a Comment