aspose file tools*
The moose likes Beginning Java and the fly likes Two classes defined in the same source file ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two classes defined in the same source file ??" Watch "Two classes defined in the same source file ??" New topic
Author

Two classes defined in the same source file ??

charlie crissman
Greenhorn

Joined: Apr 10, 2009
Posts: 3
Hello all. I'm making my way through 'Head First Java' and have encountered what seems to be a contradiction... Early on (on page 7) the book emphasizes that code structure in Java requires that a source file hold one and only one class definition. Later in the book (pg. 73) there is an example to demonstrate instance variables. I typed the code from pg. 73 into a source file and, just to see what would happen, put both class Dog and class DogTestDrive in the same source file (named DogTestDrive.java) The file compiled and ran successfully. What should I make of this apparent contradiction to the 'one class per source file' rule?? I've included the code below. It compiles successfully when class Dog and class DogTestDrive are in separate files and also when both classes are in the same file <DogTestDrive.java> Thanks in advance for your help.

class Dog {
int size;
String name;

void bark() {
if (size > 60) {
System.out.println("Wooof! Wooof!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}

class DogTestDrive {

public static void main (String[] args) {
Dog one = new Dog();
one.size = 70;
Dog two = new Dog();
two.size = 8;
Dog three = new Dog();
three.size = 35;

one.bark();
two.bark();
three.bark();
}
}

_ _ _ _ _ _

Output is:

Woof! Woof!
Yip! Yip!
Ruff! Ruff!
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
no more than one PUBLIC class per file allowed.

It is a good practice to limit one outer class per file, you can have manny as long as only one is public class.
charlie crissman
Greenhorn

Joined: Apr 10, 2009
Posts: 3
Moojid Hamid wrote:no more than one PUBLIC class per file allowed.

It is a good practice to limit one outer class per file, you can have manny as long as only one is public class.


Thanks Moojid. That makes sense. Can you explain what you mean by outer file? Not sure if that is bad forum protocol to ask another question in an existing thread.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
outer classes are the ones that are not inner classes, inner classes are classes inside a class. You should not worry about this too much at this point, just follow the book and it will show you when appropriate.
charlie crissman
Greenhorn

Joined: Apr 10, 2009
Posts: 3
Got it. Thanks for the help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
charlie crissman wrote:Not sure if that is bad forum protocol to ask another question in an existing thread.
It is bad protocol to ask an unrelated or new question in an existing thread. But threads often lead naturally from one question to another, or a question is refined, as has happened here. Then it is like a conversation leading to more detailed discussion of a topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two classes defined in the same source file ??