0

Sorry for a naive question. I learn programming in Java and have a question on the very beginning.

I follow Lesson: A Closer Look at the "Hello World!" Application, where you can found the following code.

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Having some experience in programming in C++ this code for me looks rather bizarre.

Class HelloWorldApp contains a starting point to application, for me it looks very strange, because of many points, for example, in order to get this work I thought we need somehow to evoke the method main because it is in class, however it works as it is. If I would have several classes (nor I am not sure if it's ok in java app? but, in general it should be ok), so I could define several starting points in app?

What the reason to define staring point into the class?

3
  • 1
    A main method is the starting point of a Java application. Commented Oct 6, 2013 at 16:47
  • are you mislead by the c/c++ way .. where main method exists independently ? if this is the case, then you should read that in Java methods and data variables needs to be wrapped inside an class/interface Commented Oct 6, 2013 at 16:47
  • 1) Every program has to start somewhere. 2) In C/C++, a program (typically!) starts in "root function" int main(). 3) In Java, a program is also started (either implicitly or explicitly) from main (). 4) Since Java has no "standalone functions" (only classes), main MUST be a class method. 4) Unlike C/C++, Java allows you to have MULTIPLE main functions. 5) This allows you to choose among one or more runtime entry points for you program. See my response below for more details. Commented Oct 6, 2013 at 21:06

9 Answers 9

5

Java is not C++. C++ had to preserve backwards compatibility with C, so although it is an object oriented language it still has all elements of procedural language including stand alone independent functions. For example function main(int argv, char ** arc) is an entry point to any C (and therefore C++) program.

Unlike C++ java is defined from scratch as a pure object oriented language. Therefore there are no functions there. They are replaced by methods that must belong to class. There are however static methods that sometimes are very useful. One example is main() method. Indeed you need some entry point to your program before any instance of class is created. The perfect solution choosed by Java programming language designers was is to specify special method main() with signature public static void main(String[] args) that is used by JVM as an entry point to program.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for actually explaining the difference to someone who doesn't know about the complete OO that Java is.
4

When you start a Java program, you usually tell the JVM (either on the command line or through a .jar manifest) the name of the class that contains the main method that will be called to start your program. You can have several classes with main methods, but only one gets to be "the" main method when the program is run.

Note that many Java frameworks (applets, J2ME, Android, etc.) define their starting points internally and you are not expected to provide a main method as an entry point.

Comments

2

You are right, main is a method and it has to be invoked,

what you need to understand is that, in

public static void main(String[] args)

the method main(Sring[] args) is public and static.

Remember;

  1. Public methods can be accessible anywhere in the class, package, other package, etc. (everywhere)
  2. Static methods do not need object, they are part of the class.

So, when you compile your programs, JVM looks for a public static void main() method, to start the program. This is how it was designed, and then we follow the OOPS concepts where we import classes, methods as per our use.

The keywords here is to understand that the main() method is public and static, and the first thing JVM looks is a public static main method.

Comments

1

At runtime the main() method is called by JVM(Java Virtual Machine) . Its possible to have so many classes but only one class contain main() method and app start from class contains main() metod.

Comments

0

in order to get this work I thought we need somehow to evoke the method main because it is in class, however it works as it is.

JVM : Java virtual machine looks for the main method in the java class mentioned while running the program. So calling of main method happens but as a programmer you need not to do it, jvm will do.

If I would have several classes (nor I am not sure if it's ok in java app? but, in general it should be ok), so I could define several starting points in app?

Generally there is one starting point of any application, so main method should be present in the starting class. Other classes can be used or called by the main class as and when required.

Comments

0

The JVM (java virtual machine) looks for the main method in your program, and runs that method first. You have to tell the JVM which class it is in (IDE's do this for you). The method signature must always define it as being a public static void since it cannot return anything and is accessed remotely.

Comments

0

In java, application is any class that contains method public static void main(String[] args). Usually application also uses libraries, which are collection of classes. That classes may also contain methods main, but they are not invoked automatically (but can be invoked directly by name from other classes).

Comments

0

Q: If I would have several classes (nor I am not sure if it's ok in java app? but, in general it should be ok), so I could define several starting points in app?.

A: Absolutely yes.

For example, you could have a "main()" in one class that invokes a GUI front end, and a "main()" in a different class for a command line front end; then call your program with one or the other.

Or you could have a "main()" that invokes unit test functions on your class.

Since only one "main()" will ever be executed, the ability to define multiple entry points gives you the flexibility to create different "versions" of your program from exactly the same code base.

Q: so I could define several starting points in app?

A: Yes - exactly :)

You can dynamically choose any one of them when you start your program.

2 Comments

PS: C# is like Java. Only instead of public static void main (String[] args), C# has public static void Main(string[] args)
PPS: You can specify which main() at runtime by 1) your "java" command, 2) the "run" configuration in your Java IDE, and 3) the META-INF manifest file in your .jar. 'Hope that helps!
0

The reason that you have the starting point in a class is because everything is a class.

Not sure if you're aware- Java is fully and only Object Oriented. So any and every file is a definition of an object(s). There are no functions- Java has static methods, which act as the same thing. main() is a static function so that Java works and has a starting point.

And about multiple starting points- when you run a java program, you in some way or another have to select the "starting" class. When you compile and have individual files you type (in the command line)

java mymainclass.class

If you make your program into an archive of java files- called a .jar, you use

java -cp myjarprogram.jar <path to main class>

Double-clickable .jars have a manifest file that has the location of the main class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.