This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

public static void main()  RSS feed

 
Nilanjan Sahu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read in many reputed books and also seen that a static function cal call only static members but in java public static void main() can call all functions. How?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12510
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a static method can only access static members... or constructors. Once you construct the object, you can call it's non-static methods. a common idiom you'll see is

public class SomeClass {
public static void main(String args[]) {
SomeClass myInstance = new SomeClass();
myInstanct.callAMethod();
}
public void callAMethod(){
//do something
}

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