• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

classes

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic