Can we access static variable directly from instance area?

class Variable3
{
static int x=10;
public static void main(String[] alt)
{
Variable3 v=new Variable3();
v.m1();
System.out.println(x);
System.out.println(v.x);
System.out.println(Variable3.x);
}
public void m1()
{
System.out.println(x);
}
}
/*
Output:- 10 10 10 10
Explanation:- Static variable can be accessed directly from both instance and static area.
Also static variable can be accessed either by object reference or by class name.
Within the same class we can access static variables directly and not required
to use class name.
*/

0 comments: