HI all,
I have a small doubt regarding statics in
JAVA. Consider the following
code.
public Class A
{
int x=10;
void printX()
{
System.out.println ("x is :" + x);
}
public static void main()
{
printX();
}
}
This results in a Compiler error, since the compiler detects that the
non-static ( uses instance variables) method printX() is used in static
context (static void main()). This is OK.
Using the same concept the following code must also report a Compiler
error. But it works
public Class A
{
int x=10;
void printX()
{
System.out.println ("x is :" + x);
}
public static void main()
{
Class a = new A();
a.printX(); // works fine ???
}
}
Can anyone please explain how ?
Regards,
Sarathy