• 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

Another Static Question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realize that a static method can call a non-static method by preceeding the method name with the instance name. Is there ever a situation where a non-static method can call a static method? If so, how? Similarly, can static method ever access a non-static variable outside of its method? Can a non-static method ever access a static variable?
Let me know what you think,
Thanks,
TM
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by TimD Moore:
Is there ever a situation where a non-static method can call a static method? If so, how?


A non-static method can invoke a static method any time it wants. You don't need to have an instance to invoke a static method, but you must have an instance to invoke an instance method.


Similarly, can static method ever access a non-static variable outside of its method? Can a non-static method ever access a static variable?


No, a static method can not access a non-static variable that is defined outside of the scope of that method. You first need to have an instance of the object in order to have non-static fields that are accessible. If, within the static method, however, you created an instance of the object, you could then possibly get at the non-static members of that object.
A non-static method can access a static method without problem. Again, you don't need to have an instance of the class to access a static member.
I hope that helps,
Corey
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there ever a situation where a non-static method can call a static method? If so, how?

Like this:

Similarly, can static method ever access a non-static variable outside of its method?
By preceding the variable with an instance name.

Can a non-static method ever access a static variable?

[ August 21, 2003: Message edited by: Thomas Paul ]
[ August 21, 2003: Message edited by: Thomas Paul ]
 
TimD Moore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both. That definitely helps.
TM
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic