• Post Reply Bookmark Topic Watch Topic
  • New Topic

static method question?  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??


example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??

 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a matter of other classes, it's that you can't call a non-static method from a static method, such as main(). Main has no instance so it can't call max(), which needs an instance.
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you DID want to call the max method, you would need an instance of the Test class.

The pattern I learned to apply during studying:
- shift your code from the "main" method to a non-static method.
- your main method should just create an instance of your class, and invoke the method which actually does stuff.




Also note that by convention, class names should always be capitalized, and variable names start with a lower case letter.
It just makes thing easier to read.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the help guys =) all start to make sense finally to me after a long time =) awesome forum

Adam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!