This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes public static void main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public static void main()" Watch "public static void main()" New topic
Author

public static void main()

Nilanjan Sahu
Greenhorn

Joined: Apr 27, 2008
Posts: 3
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

Joined: Oct 02, 2003
Posts: 11256
    
  16

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
}

}


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: public static void main()