• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two classes defined in the same source file ??

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks for the help!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic