File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Classes in source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classes in source file" Watch "Classes in source file" New topic

Classes in source file

JL Valor

Joined: Mar 15, 2001
Posts: 1
I am studying for the Sun Java Cert exam and this problem appeared on a mock exam.
What will happen when you attempt to compile the following in a file?
class Base{
public static void main(String[] args) {
public class Test extends Base{}
Well, I got it wrong but when i ran it myself it outputs "Hello." My question is...I though there could only be one default or public class in a source file. If that is true, then why is this code successful?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
There could only be one public top level class (ie. not an inner class) The public class should have the same name as the filename. i.e

So much trouble in the world -- Bob Marley
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Remember class test "is a" base and therefore inherits the methods of base, including main.

"JavaRanch, where the deer and the Certified play" - David O'Meara
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Only one public class per source file is the rule I heard and I only see one here.
[This message has been edited by herb slocomb (edited March 22, 2001).]
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
What were the choices for an answer?
What did you pick out of those choices, and what was given as the correct answer?
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA

<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

I think where you got confused is the other rule is that any top level class must be either public or default

Visit my download page
I agree. Here's the link:
subject: Classes in source file
It's not a secret anymore!