Skip to main content

Java source file structure

Java Source File Structure

Java Source File Structure

A Java program can contain any number of classes, but at most one class can be declared as public. If there is a public class, the name of the source file (program) and the name of the public class must match; otherwise, you will get a compile-time error.

Case 1: No Public Class

If there is no public class, you can save the program with any name (e.g., anyclassname.java), and there are no restrictions.

class A {
}

class B {
}

class C {
}

class D {
}

Case 2: One Public Class

If class B is declared as public, the name of the program should be B.java; otherwise, you will get a compile-time error saying "class B is public, should be declared in a file named B.java".

class A {
}

public class B {
}

class C {
}

class D {
}

Case 3: Multiple Public Classes

If classes B and C are declared as public and the name of the program is B.java, you will get a compile-time error saying "class C is public, should be declared in a file named C.java".

class A {
}

public class B {
}

public class C {
}

class D {
}

Notes

  • Whenever compiling a Java program, a separate .class file will be generated for every class present in the program.
  • You can compile a Java source file (.java), but you can only run a .class file.
  • When executing a Java class, the corresponding class's main method will be executed.
  • If the class doesn't contain a main method, you will get a runtime exception: NoSuchMethodError: main.
  • If the corresponding .class file is not available, you will get a runtime exception: NoClassDefFoundError.
  • It is not recommended to declare multiple classes in a single source file. It is highly recommended to declare only one class per source file, and the name of the program should match the class name. The main advantage of this approach is improved readability and maintainability of the code.

Example

Suppose you have the following code in a file named A.java (note: this violates best practices by having multiple classes in one file, but it's used here for illustration):

class A {
    public static void main(String... args) {
        System.out.print("hello");
    }
}

class B {
    public static void main(String... args) {
        System.out.print("Hi");
    }
}

class C {
    public static void main(String... args) {
        System.out.print("Bey");
    }
}

class D {
}
Screenshot example

Command: java A

Output: hello

Command: java B

Output: Hi

Command: java C

Output: Bey

Command: java D

Runtime Error: NoSuchMethodError: main

Command: java Test

Runtime Error: NoClassDefFoundError: Test

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...