• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static objects

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic