aspose file tools*
The moose likes Beginning Java and the fly likes Why main( ) is declared as static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why main( ) is declared as static?" Watch "Why main( ) is declared as static?" New topic
Author

Why main( ) is declared as static?

Ramu Rondla
Greenhorn

Joined: Oct 04, 2003
Posts: 9
I am a beginner in Java. When I am reading about access modifier static i got a doubt-
we declare main method as static but this says that in a static method one can call only static methods / static variables. How can in main we call other variables and methods?
Ramu
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Ramu,
Welcome to JavaRanch!
Short answer: what main generally does in a "real" program is create some objects, and then call their methods, letting the objects do the work. It's not that main can only call static methods -- it's that without an object to call methods on, main can only call static methods. As soon as you create an object in main(), you can call its non-static methods.
Get it?


[Jess in Action][AskingGoodQuestions]
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Just to extend Ernest's answer a bit, main() is static so that JVM can load the class without the knowledge of how an instance of the class should be constructed. It may be a little non-obvious, but if you want to call non-static method from main(), you would need to create an instance of that class itself:
Scott Ding
Greenhorn

Joined: Nov 27, 2002
Posts: 13
yes,
good reply.
read it.
Ramu Rondla
Greenhorn

Joined: Oct 04, 2003
Posts: 9
Thanks to you all..I got your point

Ramu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why main( ) is declared as static?