| Author |
classes
|
manohar reddy
Greenhorn
Joined: Oct 13, 2007
Posts: 12
|
|
Can we write multiple classes in a single .java file? If yes then, can all of them be public classes If we have multiple classes in one .java file, what should be the name of the java file?
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
manohar reddy, These sound a lot like homework questions to me. Are they? Why don't you start by telling us what you think the answers are? Have you tried to find this out by writing several classes in a single Java file? If so, what happened?
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Olivier Legat
Ranch Hand
Joined: Nov 17, 2007
Posts: 176
|
|
|
Yes, you can. However you can only have one "main" method. The file must have the same name as the class that holds the "main" method.
|
Olly
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Olivier Legat: Yes, you can. However you can only have one "main" method. The file must have the same name as the class that holds the "main" method.
Actually all the classes can have a main method. Which one will be executed depends on your command line. Try this (all in one file called MainTest.java) Compile it and then run the following commands java -cp . test.MainTest java -cp . test.MainTest1
|
Joanne
|
 |
 |
|
|
subject: classes
|
|
|