• 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 methods and instances

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've noticed that when I create an instance of the class I'm in (from within the psvm method), I get a compile error. However, if I create an instance (from within the psvm method) of ANOTHER class, it compiles fine. Is that always the case? I was thinking so, because you can't access the instance variables/methods of the class from within a static method of that class, but you can access instance variables/methods of another class from a static method in another class if you create an instance.

Am I right?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by J Recker:
I've noticed that when I create an instance of the class I'm in (from within the psvm method), I get a compile error...


I assume that "psvm" stands for "public static void main," right? If so, "main method" is a more common terminology.

There is no reason you shouldn't be able to create an instance of the class from its own main method. Can you post an example of this error?
 
J Recker
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must've gotten confused after hours of studying yesterday, because today all things work as they should. static variables can be accessed from a static method, and instance variables cannot be directly accessed from a static method.

thanks for replying. sorry to have wasted your time. back to studying...

reply
    Bookmark Topic Watch Topic
  • New Topic