0

Let's assume I do want to create a small program that uses few objects, one constructor only and a formula to calculate value of one of the fields of the objects.

Is it considered bad to use the class that also contains main, if the program is short and simple? (example below)

public class testClass{

<fields, constructors, getters/setters, methods>

    public static void main(String[] args) {
        <creating/using objects>
    }
}

Should I be creating a separate class for this, and if so, why? (example below)

class anotherClass{
    <fields, constructors, getters/setters, methods>
}

public class testClass{
    public static void main(String[] args) {
        <creating/using objects>
    }
}
1
  • your example program is very small, small enough to fit into one class
    – johnII
    Nov 5, 2016 at 13:46

2 Answers 2

0

I think it does not really matter. If a program is really small, you might as well use one class only, as every class adds some boilerplate code. In your case that additional class is not really needed.

Many of best practices are more relevant to larger programs anyway. If you have a program consisting of one class, you can refactor it in a few minutes anyway. So unless you are doing something obscure, any Java programmer will understand what you are trying to do.

0

I would say it's bad practice to add something which isn't needed. Creating a separate class just to put the main in doesn't seem like a good idea to me. If there isn't a clear reason to add something, I would avoid it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.