here is some code plz observe it 1. class abc 2. { 3. static int a=10; 4. static abc ob=new abc();//static object instantiation 5. public static void main(String args[]) 6. { 7. ob.method(); 8. } 9. public void method() 10. { 11. ob=new abc();// compiles and run 12. System.out.println(" a = "+a);// direct refence error 13. System.out.println(" a = "+ob.a);// with object still error 14. } 15. } as java says that static members can't be refenced by non-static methods. so why static objects as in line (10)are accessed by non-static method.as static variable is not at line (12,13),wheteher called directly or with the refence of object. so i can't understand this behaviour of static. can any body explain. thanx in advance.
Jyotsna Clarkin
Ranch Hand
Joined: Jan 26, 2001
Posts: 158
posted
0
Originally posted by salman baig: as java says that static members can't be refenced by non-static methods.
That statement is NOT correct. A static member belongs to the class. Static methods have access to static members only. A static method can access a non-static member by creating an instance of the class within the method. A non-static member belongs to an instance of the class. Non-static methods can access both static and non-static members.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.