class Demo{
static int x=10;
public static void main(Stirng[] args){
Demo d=new Demo();
int x=5;
System.out.println(x);//here the output comes 5(local )
}}
Here the local x hides the instance variable.
Some interesting things below
1-
class Demo{
static int x=100;
public static void main(Stirng[] args){
Demo d=new Demo();
int x=50;
System.out.println(d.x);//here the output comes 100()
}}
2-
class Demo{
static int x=100;
public static void main(Stirng[] args){
Demo d=new Demo();
int x=50;
System.out.println(d.x);//here the output comes 50()
}}
The first code has instance variable x and local variable x also both are in different.Both are in different memory location.
but in second code the initial value of x was 100 again we put the value 50 to the same x.
No comments:
Post a Comment