Question

I'm new to object oriented programming and I don't understand what's the purpose of the main.

Yes, I read that it's the "entry point" of the program but what I don't understand is what should be in the main? And what are its responsibilities?

It may happen that something written in the main could be encapsulated in another object, but how much should you use this approach?

Here is my very first main I wrote in Java, it's very simple but it may make you understand my doubt better. I have an abstract class Animal which is extended by "Cat" and "Dog". I used the main to create some object and also as an "interface" with the user, indeed as you can see I used some conditional instruction to "ask the user" what he want to do.

My question arose from the fact that the interface could be encapsulated in another object and not giving that responsibility to the main.

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("What type of animal do you want to create? \n dog cat");
    String type = input.nextLine();
    if ( Objects.equals(type, "dog")){
        System.out.println("Enter the animal's age: ");
        int age = input.nextInt(); // Scans the next token of the input as an int.
        System.out.println("Enter the animal's name: ");
        String name = input.next();
        Dog first = new Dog(name, age);
    }
    else if ( Objects.equals(type, "cat")) {
        System.out.println("Enter the animal's age: ");
        int age = input.nextInt(); // Scans the next token of the input as an int.
        System.out.println("Enter the animal's name: ");
        String name = input.next();
        Cat first = new Cat(name, age);
    }

    else{
        System.out.println("Error: the specified type does not exist.");
    }
    System.out.println("The number of animals is:" + numberOfAnimals);
}
Was it helpful?

Solution

First off, your example isn't an object-oriented program. It's a procedural program that happens to store data in objects, because that's the tool that your language (Java?) provides for structured data.

A true object-oriented program consists of objects that interact with each other -- it's about behavior rather than data (I realize that is a controversial statement, so here's a link where you can see multiple definitions of object-orientation from people with more credentials than me; note that behavior appears in most of them).

In a true object-oriented program, according to the definition I use, you have independent objects interacting with each other. The role of the main function is to create the initial objects and wire them together.

As a simple example, consider a web-application that's built on top of a database. This application could be broken into objects in many ways, but here's one of them: a Networking object that accepts connections, parses the HTTP request, and dispatches to an appropriate Controller object, which interacts with a Database object and produces the response (if you'd like to associate one or more View objects with each controller, feel free to do so). You could also add a Threadpool object to provide separate streams of execution.

The role of main in this application might be to:

  1. Create the Database object
  2. Create all of the Controller objects, and associate them with the Database object
  3. Create the Network object, and associate all of the Controller objects with it.
  4. Start the Network object running (which might also involve creating the Threadpool and wiring it into the Network).

These setup steps could be explicitly specified in main, or they could be handled by some other object. For example, in a typical Spring application, all that the main function does is create the application context (a single object). This triggers creation and wiring of all the objects mentioned in the configuration for that application context.

OTHER TIPS

Well, it depends. Take it to two extremes:

  1. Place all code line by line in your main. That will compile and run nicely. But for a human, the code will not be digestible.
  2. Just place a single function in main and call it doWhatNeedsToBeDone and eventually continue in this routine the same way. Now you have a very neat main but of course, you don't get what needs to be done.

So, the truth is somewhere in-between. Try creating a main that fits one page, so someone can get what the main instructions are that need to be performed. Getting the right border is simply a matter of experience.

The above is a general rule and applies to OO as well as functional programming.

The static main method exists as a transition from the non-object oriented world to the object oriented world. It has been done this way since at least C++ in the '80's.

Static methods are essentially non-object oriented methods: they may be procedural methods; they may be functional methods. The notion of static methods is essentially an escape of OOP to other programming paradigms.

Java, C#, and C++ all use the static main as a transition from the traditional C-style main into their respective programming language, from which you can now choose to use objects (or not) at will.

These languages could have required a primordial instance object instead of a static main, but have chosen the static main approach instead. Using an instance approach alternative, the main class would be a subclass of the thread class, and language runtime would conjure an initial object instance of the main class and then invoke its run instance method, much the way additional threads are launched/created.

But it historically has been done the other way instead: in part, these languages are just following suit, and in part, because threading wasn't a priority back in those days so they used the simpler static approach.

Also, the static main approach is "simpler" in that you don't have to use subclassing, so trivial tutorial programs like hello world are simpler in some sense (yet they use the relatively inexplicable static, which is hard to explain to someone when you're trying to first teach OOP.)

The Main() Method is used to run the program

Responsibility - Once you start running your program, the Main() Method calls other methods to start running your program.

This is a simple answer which helps me understand what the responsibility of the Main() method is.

Licensed under: CC-BY-SA with attribution
scroll top