• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Accessing Static Members

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I've always been a bit confused by when you can and can't access static methods.

Can I just confirm that this is correct:

You can access a static method or a static variable from a non-static method, but you can't access a non-static method or an instance variable from a static method. (Assuming all the relevant members are in scope)

Cheers

Joe
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try it? Just write some code and you'll find the correct answers.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done and that's the conclusion I came to, but I just wanted to confirm and maybe get some other opinions / info from some Ranchers.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well
we cannot access the instance members directly from the static methods but we can access the static members from the non static methods
reason is
  • Static members are connected to class and not a particular instance so they can be accessed by any object


  • now let us see the following program



    hopes this makes sense
     
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Joe it is correct what you think.
     
    Joe Lemmer
    Ranch Hand
    Posts: 171
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Prasad,

    Yes that makes perfect sense. Thanks very much for the commented example. I was a bit thrown by a Whizlabs question which was something like the following:



    I had thought that this would fail at compile time because a non-static method (get) was accessing a static method (get1), but I can see now that it will compile fine because there will always only be one get1() method for a class (unless it's overloaded) so thee's no confusion as to which method will be accessed as there would be for non-static methods.

    Cheers

    Joe
     
    reply
      Bookmark Topic Watch Topic
    • New Topic