This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main method

 
Indy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Indy
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic