Basic Question???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • softwaregeek
    New Member
    • Mar 2007
    • 27

    #1

    Basic Question???

    Why we can have only public class per java file? Is there any specific technical reson about that constrain? or any predefined JVM rule or else??

    Thanx ...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by softwaregeek
    Why we can have only public class per java file? Is there any specific technical reson about that constrain? or any predefined JVM rule or else??

    Thanx ...
    The compiler needs to be able to find classes that may not yet have been compiled.
    If source code for a class can have any file name, or is hidden together with other
    class sources, the compiler can't find it.

    kind regards,

    Jos

    Comment

    • softwaregeek
      New Member
      • Mar 2007
      • 27

      #3
      Originally posted by JosAH
      The compiler needs to be able to find classes that may not yet have been compiled.
      If source code for a class can have any file name, or is hidden together with other
      class sources, the compiler can't find it.

      kind regards,

      Jos

      Jos yet its not clear....plz explain it in as simple way as possible...

      thanx...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by softwaregeek
        Jos yet its not clear....plz explain it in as simple way as possible...

        thanx...
        Suppose you have two public classes A and B and also suppose an A object
        creates or uses or refers to a B object.

        When you compile A.java the compiler notices that a B object is used. Suppose
        there's no B.class yet (B has not been compiled yet). The Javac compiler starts
        searching for a source file B.java in order to compile it too. Suppose the name
        could be anything; how is the compiler supposed to find the source for class B?

        kind regards,

        Jos

        Comment

        Working...