0

I have two classes in a single java file, no internal class or any parent or child class, just two classes in a single file. I know two classes cannot be public in a single java file, they need to be declared in seperate java files. As far as I know is that if we don't specify any access specifier before the class name, the default specifier is given which is public. So if I remove the 'public' keyword from the second class which is not the main class, why does the error of not putting the class in a seperate file go away?

class A{
    
     int a,b;

     static void doSomething(int a, int b){
        int c;
        c=a+b;
    }
}

public class generateSumEg{
    public static void main(String ags[]){
        
        //call function
    }
user47
  • 21
  • 7
  • 1
    *"the default specifier is given which is public"* ... no, the default modifier is not public for classes. – Tom Jun 26 '21 at 00:03
  • 2
    `public class X` means everyone can see `X`. `class Y` means only the package in which it was declared can see `Y`. See [What is the difference between public, protected, package-private and private in Java?](https://stackoverflow.com/q/215497/9997212). – enzo Jun 26 '21 at 00:05
  • For your last question, a source file can contain 1 (unnested) public class and multiple (unnested) package-private classes. It cannot contain multiple public classes. (But your earlier assumption about the default modifier are incorrect which is the main cause of your confusion.) – Stephen C Jun 26 '21 at 00:09
  • *"As far as I know is that if we don't specify any access specifier before the class name, the default specifier is given which is public."* You are incorrect. The default access is "package private", which has no specific keyword. – iggy Jun 26 '21 at 00:59
  • Got it, thanks y'all – user47 Jun 26 '21 at 06:22

0 Answers0