1

Is there any advantage to using the following code

public void run(){
     //Code
}
public static void main(String[] args){
    new Main().run();
}

instead of

public static void main(String[] args){
    //code
}

For small programs that you write during a introduction to programming?

I can't see how this will offer any memory saving or performance?

noahp78
  • 66
  • 5
  • I can only say Java is OOP language, but its a personal preference, as far as I know. – ArsenArsen Sep 11 '16 at 09:48
  • 2
    I guess it's useful if your object is also something that is used outside of this particular application. I would have thought it would use a little bit more memory but not enough for it to make any difference. Also I guess some peoples style is to enter a non static context as soon as possible. – Paul Rooney Sep 11 '16 at 09:52
  • It's also useful to separate the part of the code that deals with parsing command line arguments from the "business logic". The former can stay in `main` (or inside of the main class), the latter can live in another class. – Thilo Sep 11 '16 at 09:55
  • Some more (not really compelling) discussion: http://stackoverflow.com/questions/33812496/java-only-static-members-in-main-class?rq=1 – Thilo Sep 11 '16 at 09:57

1 Answers1

0

For instructional purposes, or really any other purposes, the difference in memory is negligible. It's also not any more object-oriented to hide code in sub-routines, if the data that code accesses is the same either way and there are no other sub-routines.

So, unless there's actually more to your code than you're showing, I would use the simpler variant without the run function.

Myrle Krantz
  • 136
  • 1
  • 3