• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help conceptualizing Java layout - when to use objects.  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well after looking at some of the topics in the "beginner section," maybe you ought to create a "pre-beginner section" just for me...

At any rate, I am having trouble conceptualizing the layout of a simple Java program. In Javascript, it is quite possible to put a simple to middling-difficult project in one file. You can have their equivalent of multiple objects in one file. Also you can use these objects to sort of group together things. However, somehow I am overlooking some vital piece of information in my Java book. If I am reading things right, each class has it's own file, is that right? You would never put two classes in the same file??

Also, are these objects, apparently derived from classes/external files, are these objects the most suitable way to represent things in Java? I basically wanted to know how much change I currently possess in my "pocket." I wanted to keep a running tab and have the ability to add and subtract quarters, dimes and whatnot. So part of me wants to keep things simple and just have some sort of record-type thing that clumps together the various types of money. Each variable in the record-thingy is an integer. So one variable is entitled "quarters" and may possess the value 2, while another variable is called "dimes" and may be zero. Somehow I am missing the explanation of this in my book, but could somebody tell me the proper way? Does my program involve opening up another text file, name it class Pocket, and fill it with object code. Then I reference this from my main program?

Have I made this pocket thing way too complicated, and maybe I am better off just having ungrouped variables in my main class to represent nickels and dimes and whatnot?

Thanks,

Andrew
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very common beginning thoughts. No need for a special section.

You may have more than one class per file. You may only have one top-level (not nested in another class) public class per file. In early programming exercises, it's quite typical to have all 'pieces' of the program in one class. As the programmer matures and begins to write program elements that might be reused, he/she will transition to writing those elements in separate files.

For the program you've described, one might have a single class, Pocket, with fields that represent the number of each type of coin in the pocket. If the program will have only one pocket, then the program structure might be:


Later, if the programmer decides that multiple pockets will be needed (multiple instances of Pocket will be created) then the main() method can be moved to another 'driver' class and the Pocket class can be left in a file by itself to be reused as needed.

This is one possible way to accomplish what you've described. There are others.

Edit: I should have noted that the fields and methods inside the Pocket class for the template above will be static so that they can be used by the main() without without creating an instance of the Pocket class. When/if the Pocket class is broken into a separate file with the intent to instantiate it, the static fields and methods will be changed to non-static. This extra editing required to convert a class from one that is self contained with its own main() method to a class that can be instantiated multiple times is a good reason to move away from the single-file mentality as quickly as possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!