File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classes in source file" Watch "Classes in source file" New topic
Author

Classes in source file

JL Valor
Greenhorn

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 Test.java file?
class Base{
public static void main(String[] args) {
System.out.println("Hello");
}
}
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?
Thanks
JL
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 yourPublicClassName.java
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Cindy Glass
"The Hood"
Sheriff

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
JL,
What were the choices for an answer?
What did you pick out of those choices, and what was given as the correct answer?
Thanks,
------------------
  • 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: 4347
    
    2

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


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classes in source file