• Post Reply Bookmark Topic Watch Topic
  • New Topic

static functions in main  RSS feed

 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically im trying to create a program following Object Oriented Programing. Can i (or is it good practice) to create other methods in the class that has the main function in it?

So for example if I am creating a board game and have A Game class, Board and Player class with main in the Game class. should i have the instructions and basic game loop in main (seems kinda messy and not OO). should create other static methods (functions w/e) in the Game class that main calls or just create a GameHelper class?


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,
There's another option. Have instance methods in the Game class. That way main is two lines:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As much as many of my posts may paint me as an idealist when it comes to program design and OO, I don't believe in making objects just to make a program "pure" OO. When it comes to the static main() method, sure, make it high-level and devoid of nitty-gritty details. However, I'm not averse to writing a main entry point class that has all static methods that control the general flow of the program either. I see the code in a class like that as largely being a script anyway. For me, there has to be some interesting behavior, relationships, and attributes that can vary from one instance of a class to another in order for the case to go "full OO" on it compelling enough. Otherwise, instantiating a class for which there will only ever be one instance can seem artificial and forced.

That said, if it makes it easier to test and configure, going full OO isn't a bad thing either, even if you're going to have only one instance of that class.

Bottom line, use your common sense: If the code you're writing doesn't benefit from organizing around the abstractions and flexibility that objects give you, it might be simpler to just forego the ideal "pure OO" approach and write Java code that's really just a "script."
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree there are instances where it doesn't make sense to instantiate. In this case, the class is called Game. And I can easily envision a Game object having state. I think that's the determining factor for me; does the class have meaning on its own.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply guys, it seems like a dumb question but no one ever mentions it, even in Alot of books.

I think what I've decided to do for my have is instead of player class I'll have Players turn or something that handles a basic turn for any type of player, even a AI player, of course the AI player will have to have its own sub comput move or something.
Basically because the original player class only got the name symbol and move input
So I will still have a game class with the basic game loop that calls playerturn.
Sry I am typing on my phone..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!