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

main method

Indy
Ranch Hand

Joined: Apr 24, 2000
Posts: 44
And, what exactly does this main method do? I am becoming more and more confused while I learn more Java. Sometimes, seems, main is like the headquarter of the class, it command the running progress, and doesn't matter where the main method is called.
At other times, classes run pretty well without main. please excuse me for such beginner question, although I am going to take the exam soon.
Thanks in advance to all.
Indy
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
To use a Java class as an application, you need to have a main method. When this application is executed, JVM looks for the main method and executes it.
Sometimes, though it is not application a class might have main method. That is used for dubugging.
But it is not mandatory to have a main method inside a class.


Saravanan
Indy
Ranch Hand

Joined: Apr 24, 2000
Posts: 44
Thank you for your explaination. I can understand why classes hold main method. Then for those classes don't have main method, what should we describe their situation except debugging. (of course they are not used as applications)
Indy
IBRAHIM
Greenhorn

Joined: May 26, 2000
Posts: 5
Thandapani: Well, from experience, servlets and applets are java programs that do NOT contain a main method. So, from your earlier
statement, can I safely conclude that its ONLY in Applications and NOT Applets, where a main method is used?
Thanks,
Ibrahim
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
I am confused too. I was under the impression that, in an application, you should always call the main() after the class definition:
class MyClass
{
public static void main(String[] args)
{
// rest of the code
}
}
....but I've seen instances where the main() is called elsewhere, such as after initializing a few variables. Is there a specific procedure to follow or is it just a matter of preference ??
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
it is not a rule to write the main method as the first method in the class. you can write it anywhere inside the class.
e.g.
class Ex1{
float height = 0;
Ex1()
{
System.out.println("constructor called");
}
public float getheight()
{
System.out.println("returning height");
return height;
}
public static void main(String arg[])
{
Ex1 example = new Ex1();
float h = example.getHeight();
System.out.println("height returned by method = :"+ h);
}
public void setheight(float hh)
{
this.height = hh;
System.out.println("setting height");
}
}
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
The main() method is not called because of its position in the class. Let me repeat that The main() method is not called because of its position in the class.
The main() method is always called first in an application no matter where it is located in the java class. We are not writing a procedural language program. The program does not start executing at the top and work its way down.
In order to run a class as a stand-alone program, it must have a main() method. A class that is not run as a stand-alone program has no need for a main() method. These classes would have a method invoked by other classes. Applets also do not need a main() method.
 
jQuery in Action, 2nd edition
 
subject: main method