• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How a static main method, calls other non-static member methods of its class?

 
Akshay Kumar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i read in the books that static methods can access only other static variables/methods, but how is it in the case of main method?
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static method can neither access non-static variables nor can call non-static methods. main() method is no exception.
Try this code

To access the myMethod() from main, you need to create an instance of MyClass and call it.

Same case with the non-static data also.
[ February 24, 2004: Message edited by: Mani Ram ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic