2

If I am writing multiple classes on a .java file, do I need at least one public class?

But it compiles if I have more than one class without any public.

class A {
    int x=1;
    int y=2;
    void m1(int i){
        System.out.println("i="+i);
    }
}

class B extends A{
    void m1(int i){
        System.out.println("i="+i);
    }
}

class test{
    public static void main(String args[]){
        A a1=new A();
        B b1=new B();

        System.out.println(b1.x);

        System.out.println(a1.y);
        //System.out.println(A.y);

        a1.m1(4);
    }
}
Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Yasir Mohamed
  • 51
  • 1
  • 6

4 Answers4

5

No, you do not need to have a public class in any source file.

You can have at most one public class per source file. But it is not required to have at least a public class per source file.

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

My personal preference is to put one class per file, mostly for reading purposes. Only one class can be public per file but you can have as many other classes with less restricting accessors. Also take a look at nested classes maybe thats what you're trying to do.

Take a look at this: How many classes should a programmer put in one file?

Community
  • 1
  • 1
Phylax Wetos
  • 173
  • 7
  • This does not answer the question and should be a comment – Binkan Salaryman May 28 '15 at 14:13
  • why one class per file should be considered a good coding style ? It's pretty common defining restricted scope classes without cluttering the project with extra files. – BigMike May 28 '15 at 14:14
  • Thats exactly what i ment with the nested classes. They are used to hide them and only use them inside the public class. Also i answered the question with saying that only one class can be public per file but not more than that. – Phylax Wetos May 28 '15 at 14:15
  • Nested classes are a bit particular, depending on how you write 'em you may still need an encapsulating instance to be able to use 'em and that's not always possible. That's where simple private classes defined in the very same source file comes handy. The assumption 1 class per source file is good coding style is just wrong. – BigMike May 28 '15 at 14:21
  • Maybe if you really want to do that. But this seems to be not very often the case. Also i never stated that only one class per file is the ultimative solution. I just wanted to say that in most cases it should be considered and you really have to think again if you need such a private extra class. – Phylax Wetos May 28 '15 at 14:32
  • Not really, it's not a matter of coding style, it's a matter of personal taste. For an usage case of top level private classes in a single source file, the first example coming to my mind are daunting Swing Action Listeners, but if you prefer having some dozen 10 lines source files in your project, well, it's just a matter of personal taste. – BigMike May 28 '15 at 14:39
  • What is the difference between coding style and personal taste. Most or all coding style is personal taste. – Phylax Wetos May 28 '15 at 14:40
  • Then change "A good coding style would be" with "My coding style preference is". Consider also, there are times, when you have to abide to someone else coding rules, even if you don't like 'em (e.g. if the company has set coding style, you have to abide to them, even if they're nonsense). – BigMike May 28 '15 at 15:19
  • Well im really used to this rule but i will change it in my answer. Have a nice day :) – Phylax Wetos May 28 '15 at 15:23
0

Classes that are public must be implemented in a .java source file with the same name, non-public classes can reside in source files with another name.

Rajesh
  • 2,135
  • 1
  • 12
  • 14
0

The java language specification states:

The access modifier public (§6.6) pertains only to top level classes (§7.6) and to member classes (§8.5), not to local classes (§14.3) or anonymous classes (§15.9.5).

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29