Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I put main() in a dedicated class?

Tags:

java

oop

I do not have any Java experience and have a C background.

I would like to create a new data type, like an abstract data type. In C, this would be done by creating a structure for the new data type; I understand that in Java, you create a class for that new data type, and then go about creating objects for that class.

I have written this two different ways, and both of them seem to work. But I do not understand the shortcomings of the second way.

  1. Here, I create a class for the data type, create an instance of it, and then perform operations. I've found this to be the design used in tutorials on the Internet.

    public class DesignOne {
        public static void main(String[] args) {
            MyDataType obj = new MyDataType(3,4);
            System.out.println(obj.sum());
        }
    }
    
    class MyDataType {
        int i;
        int j;
    
        MyDataType(int i, int j) {
            this.i = i;
            this.j = j;
        }
    
        int sum() {
            return this.i + this.j;
        }
    }
    
  2. Here, I create an instance of the class in the same class where I have my driver function. Operations like sum are also defined as members of the class. Basically, there's just one class.

    public class DesignTwo {
        int i;
        int j;
    
        DesignTwo(int i, int j) {
            this.i = i;
            this.j = j;
        }
    
        int sum() {
            return this.i + this.j;
        }
    
        public static void main(String[] args) {
            DesignTwo obj = new DesignTwo(3,4);
            System.out.println("sum == "+obj.sum());
        }
    }
    

What are the shortcomings of DesignTwo, and why is DesignOne preferred?

like image 622
tubby Avatar asked Oct 26 '15 14:10

tubby


People also ask

Why does a class need a main?

Because that is how the language was designed. The JVM first loads the class containing the main function and then calls the main() method. Without loading the class, main() cannot be called, i.e, main() doesn't exist without its enclosing class. Save this answer.

Why should we enclose main method within a class?

Main method is the entry point of the execution in Java. When we execute a class JVM searches for the main method and execute the contents of it line by line. If you observe the following example you can compile a this program but if you try to execute it you will get an error saying “Main method not found”.

Is it necessary for a class to have main method?

It is not necessary for all the classes to have a main method. main method is used as an entry point for java applications. So once you have entered the java code using main method of a single class you can call other classes code form there.

Why does Java need a main?

To compile a program, you doesn't really need a main method in your program. But, while execution JVM searches for the main method. In the Java the main method is the entry point Whenever you execute a program in Java JVM searches for the main method and starts executing from it.


2 Answers

It essentially boils down to Abstraction. You should try to give each class one role. Here, you have two roles that need to be filled:

  1. Load an object and display it
  2. Represent an object

Therefore you should use two classes. The advantage becomes more obvious once you start to want to use and display more than one type of thing in your program.

like image 151
JoeyJubb Avatar answered Nov 10 '22 21:11

JoeyJubb


There's no inherent difference between the two (though non-final public fields are usually discouraged generally). The only difference is where you put the main method, which doesn't conceptually belong to any specific class. In larger programs it usually lives in a separate launcher class just because it doesn't make sense to tie it to some arbitrary class, but that's mostly irrelevant for data modeling.

like image 39
chrylis -cautiouslyoptimistic- Avatar answered Nov 10 '22 19:11

chrylis -cautiouslyoptimistic-