Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

lack of public class in a file

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, here is a rather confusing question from Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 9041-9049). Enthuware. Kindle Edition.

The options below contain the complete contents of a file. Which of these options can be run with the following command line once compiled? java main
Select 1 option
A.
B.
C.
D.

I went for option D, not entirely sure why, it seemed more correct and I got that right. But then, when I read the explanation I thought about it again and realized that it doesn't sound so right after all (the explanation doesn't say much anyway so I didn't copy that over). What doesn't convince me is the fact hat there isn't any public class in D and I always thought that a file to compile HAD to have a public class which matched the name of the file, but that's not happening in D. Any idea?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3889
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not necessary for a java file to have a public class. The requirement is that if a class (or enum) is public then that class (or enum) must be defined in a file by the same name and that there can be only one public class (or enum) in one file.

This explanation has now been added to the question.

HTH,
Paul.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I see, thanks for clarifying that
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!