Source file naming rule in java - When source file consists of public class & non-public classes OR only non-public classes


Rules for declaring classes in a java Source file >
  • Only one public class is allowed in one source file.
  • Source file name must be same as public class in a file .
    • Example - If file public class = public class Abc{}, then file name must be Abc.java
  • Multiple non-public classes are allowed in one source file.

  • If class has been defined inside some package >
    • then package should be the first line in the source file.
  • If certain imports have been used in class then >
    • import must be followed by package declaration.
    • If  class has not been defined in any package, then import should be the first line in the source file.


Let’s understand the above rules practically using programs >

Before reading below, you must go through Writing and executing first java program through CMD in windows, Setting up ECLIPSE in java, Directory of .class file




Let's see source file name when >
1) When source file consists of public class  & non-public classes>
2) When source file consists of only non-public classes>



1) When source file consists of public class  & non-public classes>

  • Source file can only be named as Abc.java, because it consists of public class Abc (if any other name than Abc.java is used, we will face compilation error),

  • Also source file cannot have more than one public class, but can have any number of non-public classes.

  • non-public classes does not have any impact on naming of source file.


Let’s say below code in file =  E:\Abc.java
/* Source file can only be named as Abc.java, because it consists of public class Abc
* (if any other name than Abc.java is used, we will face compilation error),
* Also source file cannot have more than one public class,
* but can have any number of non-public classes.
*
* non-public classes does not have any impact on naming of source file.
*/
public class Abc {  //public class
   public static void main(String[] args) {}
}
class MyClass1 { //non-public class
   public static void main(String[] args) {}
}
class MyClass2 { //non-public class
   public static void main(String[] args) {}
}


Go to CMD and type below commands >
E:\>javac Abc.java

E:\>

As soon as javac Abc.java is executed 3 .class files are formed in directory =
E:\Abc.class
E:\MyClass1.class
E:\MyClass2.class


2) When source file consists of only non-public classes>
  • If Source file does not contain any public class it could have any name.
Example - Xyz.java, MyClass.java, etc..
  • As no public class is there in Source file, it could be named on non-public class name also >
         MyClass1.java     or
         MyClass2.java     as well.


Let’s say below code in file =  E:\Xyz.java
/* If Source file does not contain any public class it could have any name.
* Example - Xyz.java, MyClass.java etc..
*
* As no public class is there in Source file, it could be named on non-public class name also >
*         MyClass1.java     or
*         MyClass2.java     as well.
*
*/
class MyClass1 {  //non-public class
   public static void main(String[] args) {}
}
class MyClass2 { //non-public class
   public static void main(String[] args) {}
}


Go to CMD and type below commands >
E:\>javac Xyz.java

E:\>

As soon as javac Abc.java is executed 2 .class files are formed in directory =
E:\MyClass1.class
E:\MyClass2.class



You Must read : How .class files are formed when we use >

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

RELATED LINKS>

Java tutorial - Advantage, Where is java used in real world, platform independent language

Writing and executing first java program through CMD in windows, Setting up ECLIPSE in java, Directory of .class file


Labels: Core Java
eEdit
Must read for you :