This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is there away I can have two or more classes in same file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there away I can have two or more classes in same file?" Watch "Is there away I can have two or more classes in same file?" New topic
Author

Is there away I can have two or more classes in same file?

hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
Is it possible to have two ormore classses in the same file?
Jaydeep Singh
Ranch Hand

Joined: Oct 02, 2000
Posts: 43
yes Mr. Mehata, you could have, but make sure there will be only one public class with in each source file.
If you look at Inheritance or composition you will get your answer.
Examle:

[ September 30, 2003: Message edited by: Marilyn de Queiroz ]
Anil Kumar
Greenhorn

Joined: Jun 19, 2000
Posts: 13
you can more than one class in file. But that would be inner class. Which have limited acccess. It can access all the variable, method from outer class.
hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
Thanks Jaydeep amd Ajith. What would be the advantage of doing this though? I am not very clear.
Jim Kilthau
Greenhorn

Joined: Oct 06, 2000
Posts: 14
No offence, but Anil is incorrect. You can have as many classes in one program as you want. The reason you can only have only one public class is because the public class name is the same as the source file name. Of course you can't have two source files with the same name in your source directory. Otherwise the public class and the rest of the classes in your program are no different in terms of status or ability.
In this program, you have two seperate and individual classes;
_______________________________________________________________
public class TestIt {
public static void main(String stuff[]) { //program starts here
OtherClass OC = new OtherClass(); //create object
}
}
class OtherClass {} // do nothing
_____________________________________________________________
When you compile the above, you must name the source file TestIt.java, because that's the name of the public class. The
compiler will create two class files; TestIt.class & OtherClass.class. These two will be used (interpreted) by the JVM when you run the program.
Here's a program with an inner class, named InnerClass;
____________________________________________________________
public class TestIt {
public static void main(String stuff[]) { //start to run here
OtherClass OC = new OtherClass(); //create object
class InnerClass {} // do nothing
}
}
class OtherClass {} // do nothing
____________________________________________________________
This class is subordinate to TestIt,unlike OtherClass. When you compile the above program, you get the two original class files, along with this one: TestIt$1$InnerClass.class. You can see by the name that this class has an inherent dependency on the outer class.
If you really want to get a good grasp of all of this, I'd recommend either of the following excellent books: Programmers Guide to Java Certification (Mughal-Rasmussen) or The Complete Java Reference (LOTS of great fundamental exercises) by Naughton-Schildt.
Have fun and let me know if I can help
------------------
Jim Kilthau
Greenhorn

Joined: Oct 06, 2000
Posts: 14
I'm not sure if I made this clear in my reply, but... the other,
"non public" classes in your program DO NOT have to be inner classes.
Jaydeep Singh
Ranch Hand

Joined: Oct 02, 2000
Posts: 43
Hello Mehta Dear,
One Example of Inheritance in Real life , or what is the need of the inheritance is???

As per our Experince in software industry it's hard to say that whatever you have done that work(object-class) is final, tomorrow you may need some Businees expansion which is forcing you to extend your software(existing(base)object), to meet the new requirement.
So Inheritence will allow you not disturb the existing users of the class, Let's make new one(derrived class). That's how thw inheritance take place.
Hope I am able to explain it.,If i am wrong feel free to correct me i'll be more happy.
jaydeep
John Croucher
Greenhorn

Joined: Sep 29, 2003
Posts: 9
I know this is an old thread, but I thought I'd still go ahead and add some information to it in case anyone else comes across it in a search.
While it is true that you can have more than one class in a file, only one can be public. The public class name should match the filename.
Also, I believe that the public class should be the one that contains main method().
Hope this helps!


John M Croucher<br />Athens, OH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there away I can have two or more classes in same file?
 
Similar Threads
two URLConnection
main() method...
SAXParseException: for 2 following marshals
More than one portlets in a single war file
Class not found error from deployed servlet