Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static objects" Watch "static objects" New topic
Author

static objects

salman baig
Greenhorn

Joined: Mar 26, 2001
Posts: 7
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
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.

 
Consider Paul's rocket mass heater.
 
subject: static objects