Originally posted by Varuna Seneviratna:
The above program works and the size variable which is declared in the Dog class in a non static context is accessed and it works
It works because you are creating an object of class Dog in static method main(), which in turn access non static 'bark()' method , which is perfectly legal !!
Originally posted by Varuna Seneviratna:
But The below program generates an error as "non-static variable x cannot be referenced from a static context pvb.go(x);".The above program accesses an non static variable from a static context and the only difference is the access statement is in a another class.But when the non static variable and the static method which accesses it is in the same class it is not possible to access the non static or instance variable.What is the theory behind the accessing static and non static variables from static and non static contexts
Here 'x' is an instance variable , and you can not access it from static main() method !
[ September 30, 2008: Message edited by: Sagar Rohankar ]
[edit]Add newlines and/or + to very long lines. CR[/edit]
[ September 30, 2008: Message edited by: Campbell Ritchie ]