Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

main method  RSS feed

 
Derek Williams
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have had one training session on Java and I am trying to make a simple game to improve my skills. I am breaking free of procedural programming and I am trying organize my code. I have a player class, game runner class, game data class, but I am sure not all need to be in classes. I am trying to think through what should be a class and what should be a method and what should go in the main.

Any insight or examples?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are some who say your main method should create an object, then call it's run() (or whatever) method.

while that may be a little extreme, generally you don't want much in there.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Basically, main should serve as an entry point to get things going. After that, things should be handled by objects.

In defining classes, try to keep your objects cohesive in terms of fulfilling specific purposes. Your application might "do" a lot of things, but it should probably have different objects that specialize in the details. When the word "has" comes up, it's a good indication that an object might be needed. For example, a game has players.

You're definitely on the right track in the way you're thinking. Just don't dive in too deep and get frustrated. (Remember, these objects will probably need to send messages to each other, so you don't want to weave a tangled web of references.) Keep it manageable. Post back with more details as you work on this.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of all things, when I try to put too much into main() I always wind up stepping on the old "referencing instance variables from a static context" problem. THAT, though, is just because I am dumb!

But, if you make a practice of instantiating and calling a startup method of some sort it can help you to avoid that because you are stepping into an instance method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!