Java Source File Structure


It is used to describe that the Java Source Code file must follow a scheme or structure. The maximum number of classes that may be declared as public in a Java program is one. If a public class exists, the program’s name and the name of the public class must match for there to be no compile time errors. There are no limitations when using any name as the name of the Java source file if there is no public class.

In this article, we will see some instructions that a Java program must follow.

Structure of Java Program

  • package statement: In Java, a package is a way to gather together classes, sub packages, and interfaces.

  • import statements: A package, class, or interface is imported using an import statement.

  • class definition: A class is a passive entity that serves as a user-defined blueprint or template from which objects are formed.

Example

package example;     //package
import java.util.*;   //import statement

class demo
{                     // class definition
   int x;
}

Important points to consider while using it

Following are the points that should be kept in mind while working with Java Source File:

Number of Classes

One neat capability of using Java programming language is having various types of classes coexisting in the same software system interchangeably. However, among these different types of classes in our application - only one will occupy the role of being a "public" face towards external users and applications accessing this software system from outside its boundaries.

As you design and organize your files into logical categories for better management - keep in mind that whichever class you mark as publicly visible carries an important responsibility; representing how other programs or modules engage with your software system at large constitutes among other things.

Source code

//declaring classes
class example {

}
class learning {

}
class machine {

}

Name of the Java Source File

Any name can be used for the Java Source File, as long as no class has a public declaration. If there are no classes that are publicly defined, Java permits us to name the Java source file with whatever, However, if a class has a public declaration, the Java source file’s name must match the public class name. The Java source file must have the Java extension.

Source code

// declaring classes
// declared as public

public class example {

}
class learning {

}
class machine {

}

Number of .class files in a Java Source File

The number of classes defined in the java program equals the number of .class files created. The javac compiler creates as many .class files during program compilation as there are classes defined in the Java source file. The Java Virtual Machine (JVM) executes the Java bytes contained in the class file. The Java Virtual Machine (JVM) is able to load the standard system classes’ class files as necessary since it is aware of their location.

Syntax

Class_name.class: class_name is name of the class that is present in the Java Source File

Source code

// declaring classes
class waytoclass {

}
class learning {

}
class programming {

}

Combined functioning

We have created three empty classes: waytoclass, learning, programming out of which waytoclass class is declared as a public class.

Source code

// file name: waytoclass.java
//package declaration
package example;

//import statements
import classes;

//class declaration
public class class1 {

}
class class2 {

}
class class3 {

}

Example

// package statement
package example;

//three empty classes
//waytoclass is declared as public
//we can’t declare more classes as public
//(at most one class can be declared as public)
//so, file name will be waytoclass only

public class waytoclass {

}
class learning {

}
class programming {

}

Conclusion

Every time we compile a Java program, a separate .class file is created for each class that is present in the program. The main method of the relevant Java class is run whenever we execute that class. If the class lacks a main method, we will see a run-time error that reads NoClassDefFoundError: main. Runtime Exception with the message NoClassDefFoundError will be raised if the associated .class file is unavailable. It is strongly advised to declare only one class per source file and to keep the class name and program name the same. Declaring numerous classes in a single source file is not advised.

The key benefit of this strategy is that the code will be easier to understand and maintain.

Updated on: 01-Aug-2023

557 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements