When you code a class for an object, do you code it in the same file as the driver class or do you code it in a seperate file? I'm confused as to where to do this because my book didn't explain.
Printable View
When you code a class for an object, do you code it in the same file as the driver class or do you code it in a seperate file? I'm confused as to where to do this because my book didn't explain.
It's common practice to split them up. The driver class(in my opinion) should just instantiate and run:
Then the actual class:Code:public class Driver
{
public static void main(String[] arguments)
{
Car c = new Car();
c.start();
c.changeGears();
}
}
Code:public class Car
{
//methods
//data
//whatever
}
Does it matter which classes you place in which files? If you have a bunch of small related classes you could group them all in one file. If you have several large classes you may wish to place them in separate files.
Yeah but if you split them up into seperate files, is there a different file type you save the class as and how do you import the class into the file that contains the driver class?
I should never have started, I don't know the first thing about Java :D
But, see if this helps you at all.
That didn't really help any. ^_^ Thanks for the effort though.
Every publically visible class must be in its own file in Java. (Package-visible classes may be in those files too, but I'm not sure whether that's a good idea.)
So I type in an object class in a seperate file and it's visible to all driver classes that I write?
It's like this: If the 'object' classes are in the same directory, then you can simply use aggregation (creating an instance). If they are not in the same directoy, then you should probably have it in a com folder. I'd personally say save the packaged classes in the com folder for utility purposes.
Say you have two classes: Dog and Cat. If they are in the same directory:
That kind of structure would allow you to create instances of each other (as long as they are not private).Code:
[ Directory ]
| |
Dog Cat
You can always create instances of other classes - you just need to use imports or fully qualified names.
Thanks you guys. I figured out how all that worked. Thanks a lot. My book didn't really explain how that really worked.