• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Nested classes  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to find an answer to my question after 30 ish minutes of google searching...
My question is how should I use nested classes to organize my code?
My code example looks something of the like: (Note : I have 1000 more lines in the Game class, I'm just cleaning it all up)

I need instances of Game but I do not want to create an instance of Logging inside each Game instance. I only want to use the methods in Logging but, log cannot be static.
Should I declare Logging abstract? static? Something else? Or should I not do this only for organizational purposes?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It all depends what you are going to do. Please explain exactly how your logging is going to be carried out. You may find you have got your solution by the time you finish that procedure. That is called rubber‑duck programming. If you haven't got the solution, the fact that we have a clear description will help us help. As it is, I am afraid I am not quite sure how you plan to to the logging.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Morris wrote:I can't seem to find an answer to my question after 30 ish minutes of google searching...
My question is how should I use nested classes to organize my code? [...] I need instances of Game but I do not want to create an instance of Logging inside each Game instance. I only want to use the methods in Logging but, log cannot be static.

OK, well first: there's a big difference between logging and a logger.

The last is a class that simply writes out messages to a logging device or file - and while I can quite understand you not wanting to create a new a new one of those for each Game, the business of logging (ie generating log messages) IS probably tied to an individual Game; so wouldn't it be nice to simply re-use Game methods to compose messages?

Should I declare Logging abstract? static? Something else? Or should I not do this only for organizational purposes?

If it was me, and Logging is concerned with generating messages to output to a "log", then I'd make it an inner class of Game that contains a Logger (ie, the place to write the messages to). An alternative would be to make it a static nested class and pass the Game instance when you create it, but to me the first option is slightly clearer, design-wise - although honestly, there's very little to choose between them.

But the important thing is to separate the logging from the logger.

And BTW, there's a pile of very good packages out there for that latter class - just one of them being log4j.

HIH

Winston
 
Nick Morris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if this posted is in the wrong forum, I came from stackoverflow and beginning Java was like generics and stuff on the collections framework.

The main is outside of the Game class
startLog() is called after a new game has been created or has been loaded successfully.
My inputs for what piece you want to move and where you want to move to is in the main. Once I have those I pass those to a function that checks to see if the move is valid
I check the value in the main to see if it is valid (it returns 0 if it is, 1 if not)
If it is 0, the piece is moved, and the AddToLog function is called with those same coordinants, which adds some stuff to the log.
If the game is over (some condition checking) I output the log via a String function called "OutputLog" which returns the log.

I don't require that the logging functions need to be in their own nested class but, it would increase readability.

And for context, what this is for is command line chess.
 
Sheriff
Posts: 4295
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this have to be a nested class? Why not an external class with an instance variable?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Morris wrote:My inputs for what piece you want to move and where you want to move to is in the main....

Well that sounds wrong right there.

The only things that a main() method should normally do is:
1. Create an instance of the class it's written for (in your case Game).
2. Launch it.

Everything else - other than possibly a few set-up parameters (which is what String[] args is for) - should be done by your Game class; and the only possible thing that main() might need to log is a failure to launch it.

For more information, have a look at Main Is A Pain

Winston
 
Nick Morris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should I not have anything in main except for a function that does exactly what my main does now? Seems kinda pointless...
 
Knute Snortum
Sheriff
Posts: 4295
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Morris wrote:Why should I not have anything in main except for a function that does exactly what my main does now? Seems kinda pointless...

I admit it does on first blush, but with anything other than a trivial program, it becomes difficult to write and debug a program in main(). One problem is the methods all have to be static. Another is that your code starts to pile up and get hard to maintain. It's just a good habit to get into.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Morris wrote:Why should I not have anything in main except for a function that does exactly what my main does now? Seems kinda pointless...

Because you're writing a Java program, not a C one.

I think Knute covered most of the points, but the fact is that it's just bad practise. Sure, you can do everything in main(), but that just makes your program a great 'lump' of code (which is why the style is called "monolithic").

And it begs the question: Why does Java force you to put it in a class? Why not just write a lump of code and run it? That's what many older languages allowed you to do.

Answer: Because Java is Object-Oriented. It wants you to write classes, and then use objects of those classes to do wonderful things. And the fact is that if - as you will eventually - write an application that has 50 classes in it, only ONE of them needs a main() method.

It's a necessary evil because the language needs somewhere to start, but trust me, you should "get outta Dodge" (or main()) as soon as you possibly can.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!