• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic