This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two classes defined in the same source file ??

 
charlie crissman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks for the help!
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic