File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods and instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static methods and instances" Watch "static methods and instances" New topic
Author

static methods and instances

J Recker
Greenhorn

Joined: Aug 26, 2006
Posts: 18
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?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
J Recker
Greenhorn

Joined: Aug 26, 2006
Posts: 18
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...

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: static methods and instances
 
Similar Threads
Static and Non-static question
[this]....
Variable Initialization
static members
Variables assigned to objects