Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2016
    Posts
    41
    Rep Power
    0

    Default Why can source files have only one public class?

    Each source code file must have one public class and can have many non-public classes.


    1. What is the purpose of each source code file having only one public class?

    2. If two or more public classes were somehow allowed, what would be the downside of that?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: Why can source files have only one public class?

    You general statement is not true. A source file may have only non-public classes.
    For your actual questions, it will be easier if you read the following. Go to page 194.

    https://docs.oracle.com/javase/specs/jls/se10/jls10.pdf

    You may want to bookmark the specs URL. These documents are not an easy read but they provide
    more esoteric details of the language.

    One quick answer is that it makes association of class files with their source code much easier. Also
    improves lookup time when a particular public class is needed.

    Interesting though. I've been using Java a long time and never even asked the question or really cared
    why. But now we both know. :)

    regards,
    Jim
    Last edited by jim829; 06-27-2018 at 03:14 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Oct 2016
    Posts
    41
    Rep Power
    0

    Default Re: Why can source files have only one public class?

    Quote Originally Posted by jim829 View Post
    You general statement is not true. A source file may have only non-public classes.
    Jim,

    Thank you for your reply. Maybe I have the wrong idea about what a source file is. When you open Eclipse, you can insert a new file which appears as a page with a tab on the top. That is a source file right?


    See the section called "Source File Declaration Rules" on this website. It says only 1 public class per source file and that you can have many non-public classes.

    https://www.tutorialspoint.com/java/...ct_classes.htm

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: Why can source files have only one public class?

    Yes, that is a source file. And yes only one public class per source file. But none of the classes are syntactically required to be public.

    More simply stated. A source file is a .txt file created with either Vim or Notepad or similar text editor (not Word or word processor) which simply has
    a .java extension. The following code is in the source file MyClass.java. When it is compiled it generates six .class files with names that
    match the line comment for each class.

    Note:
    1. interfaces and their method signatures are implicitly public (I didn't have to declare them as such).
    2. You can instantiate an interface as an anonymous class (the compiler creates a class that implements the interface)

    Java Code:
    interface Foo<T> {  // Bar.class
       public T getFoo();
       public void setFoo(T v);
    }
    
    
    interface Bar {} // Foo.class
    
    
    class MyClass implements Foo<Integer>{ // MyClass.class
        private Integer a;
        public Integer getFoo() {
           return a;
        }
        public void setFoo(Integer b) {
           this.a = b;
        }
        
        public void SomeMethod() {
             Bar b = new Bar() {}; // MyClass$1.class (anonymous class)
        }
        class DoNothing { // MyClass$DoNothing.class (inner class)
        }
    }
    
    
    class StillDoNothing{ // StillDoNothing.class
    }
    Regards,
    Jim
    Last edited by jim829; 06-28-2018 at 05:05 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Oct 2016
    Posts
    41
    Rep Power
    0

    Default Re: Why can source files have only one public class?

    Quote Originally Posted by jim829 View Post
    Yes, that is a source file. And yes only one public class per source file. But none of the classes are syntactically required to be public.

    More simply stated. A source file is a .txt file created with either Vim or Notepad or similar text editor (not Word or word processor) which simply has
    a .java extension. The following code is in the source file MyClass.java. When it is compiled it generates six .class files with names that
    match the line comment for each class.

    Note:
    1. interfaces and their method signatures are implicitly public (I didn't have to declare them as such).
    2. You can instantiate an interface as an anonymous class (the compiler creates a class that implements the interface)

    Java Code:
    interface Foo<T> {  // Bar.class
       public T getFoo();
       public void setFoo(T v);
    }
    
    
    interface Bar {} // Foo.class
    
    
    class MyClass implements Foo<Integer>{ // MyClass.class
        private Integer a;
        public Integer getFoo() {
           return a;
        }
        public void setFoo(Integer b) {
           this.a = b;
        }
        
        public void SomeMethod() {
             Bar b = new Bar() {}; // MyClass$1.class (anonymous class)
        }
        class DoNothing { // MyClass$DoNothing.class (inner class)
        }
    }
    
    
    class StillDoNothing{ // StillDoNothing.class
    }
    Regards,
    Jim
    Thank you for your reply. A lot of that is beyond my current level but I'm sure it will make sense as I get through my books.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: Why can source files have only one public class?

    Thank you for your reply. A lot of that is beyond my current level but I'm sure it will make sense as I get through my books.
    I understand. Bottom line is that for every class or interface declared in a .java source file, a separate .class file will be generated.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Join Date
    Oct 2016
    Posts
    41
    Rep Power
    0

    Default Re: Why can source files have only one public class?

    Quote Originally Posted by jim829 View Post
    I understand. Bottom line is that for every class or interface declared in a .java source file, a separate .class file will be generated.

    Regards,
    Jim

    Thank you. I'm glad you mentioned this because up until now, I always thought there was one class file generated for every source file.

Similar Threads

  1. Replies: 8
    Last Post: 10-21-2014, 12:34 PM
  2. source code in multiple .class files
    By vastrolorde in forum New To Java
    Replies: 23
    Last Post: 01-04-2013, 04:49 PM
  3. Replies: 12
    Last Post: 12-12-2010, 06:04 PM
  4. How to Compile JavaScript source into Java class files
    By tosreejith in forum New To Java
    Replies: 0
    Last Post: 04-06-2009, 03:12 PM
  5. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •