File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods and instances

 
J Recker
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic