Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you incorporate Java's main function into your design?

Tags:

java

oop

You can actually just skip this first part cause it gets confusing, but I'll keep it here cause I want to know if anyone else feels the same way. I am a CS undergrad who has have been using Java for 3 years, and I still find it difficult to comprehend how to include the Main function into my design. It feels wrong to place it in a class all on its own, but it also feels wrong to insert it into another class file. I see the latter as such because it doesn't fit in with the rest of the class making it incohesive. I can't just stick a main function on my DocumentReader object, for example. It would have nothing to do with the object. On the other hand, I can't just make a Main class that only has a main function inside it because in object-oriented programming, you're supposed to think in terms of objects effectively creating a miniature world in your head. For that miniature world to revolve around a single object that does nothing but exist just seems off. It doesn't act as a narrator and character in a story because it does nothing, at the same time. I prefer C's style of having a Main function that is separate from everything. It keeps the narrative of the story separate from the characters that are interacting with one another.

I want to know how the professionals mix the main method into the rest of their code. How do you make it fit in with the rest of the design. Also, are they generally long or short?

like image 579
cesar Avatar asked Aug 20 '10 16:08

cesar


People also ask

What is the use of main method in Java?

Java main() method. The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program. The syntax of the main() method is: public: It is an access specifier. We should use a public keyword before the main() method so that JVM can identify the execution point of the program.

Where is the starting code for a Java program located?

To sum up, the starting code for a Java program is contained inside a main function (or method). This main function is itself contained inside a class. Finally, this class itself belongs to a package.

How do you define a function in Java?

All the functions must be defined within a class. By that, we can summarize by defining a Java method as a function belonging to a class. A function is a named unit of code that can be invoked anywhere in the class. Access specifier – this shows the scope of availability of a fuction.

How to call main () method without creating an object in Java?

We should call the main () method without creating an object. Static methods are the method which invokes without creating the objects, so we do not need any object to call the main () method. void: In Java, every method has the return type. Void keyword acknowledges the compiler that main () method does not return any value.


1 Answers

I keep the main() function in a class named as my project. With this when you launch the application with the command line you type :

java com.domain.project.ApplicationName

It seems to me logic to have a simple Launcher name rather than :

java com.domain.project.AClassWhichDoesntLookLikeAMainClass

or the (too) classic

java com.domain.project.Launcher

But don't bother too much for this class, it will probably never be executed by the end user like this. A script or another executable will launch it most of the time.

Another thing, your main() method can get heavy, especially if you really use main args for more CLI options. Maybe it deserves its own class.

like image 127
Colin Hebert Avatar answered Oct 29 '22 04:10

Colin Hebert