File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classes

 
manohar reddy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic