Java class convention dealing with arraylist

137 Views Asked by At

My question is in regards to better convention for class design. I am currently working on a project for school where I am to use a class to create an object for a unit conversion calculator. I want to store these objects into an arraylist.

My question is, does it matter if I create and .add to the arraylist from the main method, or is it better to think about using the conversion class to deal with the array?

2

There are 2 best solutions below

0
Noymul Islam Chowdhury On

It is better to create a separate class. It is very good practice and help you a lot to maintain when your project will grow in future.

0
Andrew Tobilko On

The main method is a starting point for execution. That's a place only for processing application arguments (args) and creating an instance that represents the whole application*. Normally, the class that contains main should be stateless.

A good example - a Spring Boot initialiser:

public class Runner {

    public static void main(String... args) {
        SpringApplication.run(Runner.class, args); // all the magic is in there
    }

}

* Sometimes, to adhere to the single responsibility principle, we direct args handling and initialization of the principal object to different classes.