• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Static members

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer to both static and non-static members from within a non-static method but not the other way round
Is the above statement correct?
(There is no need to use exclamation marks in your subjects )
[ May 09, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would bet on True.
Static method has access to only static fields as they do not work with an instance of the class. If a method needs to access a non-static instance field of an object, it cannot be a static method.
Sandeep
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can access the static members from a static method directly.Also I can access that by creating an instance of class.Am I rite?Thanks
 
Sandeep Advani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nopes. Static members belong to a class and do not change from one instance of a class to another. You can use them without creating an instance of a class. If you create an instance of a class and access the static members, no compilation error but changes to static variables will be reflected everywhere. Remember, main() is static and you do not create an instance of the class, neither does the Java interpreter. Because of this, main can only access static instance fields in the class. To refer to non static fields, it creates an object.
Also look at the Math library, all of its methods are static. You call this methods using its fully qualified name i.e. Math.floor().

Hope this helps.
Sandeep
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got ur point. Now coming back to the first statement
You can refer to both static and non-static members from a non-static method but not the other way around.
The other way is that I can refer to static and non-static members from a static method too.For eg:main method is static and I can refer to both static and non-static members.Static memebers by direct access and non static members by creating an instance of class.Am I rite?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're right and wrong both.
You can access an instance of a class you create in a static method even if that instance is of the same type as the class in which the static method resides.
That does NOT mean you can access non-static methods from the class directly from a static method if only you have an instance of the class, all such access MUST be through that instance.
e.g.

Won't work but the seemingly similar

will because you're accessing y on the instance x of X and not on the class object which is what happens when you access the static method z().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic