The structure of a Java program is organized into distinct components that work together to define how the program operates. At its core, every Java program includes a class definition, the main method, and the entry point for execution. In addition, programs often use packages and comments to improve organization and readability.
1. General Structure of a Java Program
A simple Java program typically includes:
- Package Declaration (optional)
- Import Statements (optional)
- Class Definition (mandatory)
- Main Method (mandatory for standalone execution)
- Statements and Expressions
Example: Hello World Program
// Step 1: Package Declaration (optional)
package com.mypackagename;
// Step 2: Import Statements (optional)
import java.util. * ;
// Step 3: Class Definition
public class HelloWorld {
// Step 4: Main Method
public static void main(String[] args) {
// Step 5: Statements
System.out.println("Hello, World!");
}
}
1. Package Declaration
- Packages are used to organize classes.
- Must be the first statement in the program (except comments).
package com.mypackagename;
2.Import Statements
- Used to access classes from other packages.
- Example:
- You can also use
import java.util.*;
to import all classes from a package.
import java.util.Scanner;
3. Class Definition
- A class in Java is a blueprint or template for creating objects.
- It defines the data (fields or variables) and behavior (methods) of those objects.
- In fact, every Java program must have at least one class. If declared
public
, the class can be accessed from other packages. - The class body is enclosed in curly braces
{}
and typically contains variables, constructors, and methods.
Syntax Example:
public class MyClass {
// fields (data members)
// methods (behavior)
}
4. Main Method
- The main method is the entry point of any standalone Java application.
- The JVM always starts program execution from this method.
- It must follow the exact signature:
public static void main(String[] args) {
// code to execute
}
Explanation of keywords:
public
→ makes the method accessible to the JVM.static
→ allows the method to be called without creating an instance of the class.void
→ means the method does not return a value.String[]
→ is the data type used to represent an array of text values in Java. The square brackets[ ]
indicate that it is an array type.args
→ is the name of the method parameter of typeString[]
. This allows themain
method to accept multiple text inputs (command-line arguments) when the program starts. These arguments are typically used to pass user input or configuration data to the program at runtime.
Entry Point of Execution
📌 Frequently asked questions about the Java main()
method —👉 Click here to read more
- When you run a Java program, the JVM looks for the
main
method inside the specified class. - The code inside
main()
is executed sequentially until the program finishes or terminates early. - Inside
main
, you can create objects, invoke other methods, or perform operations.
5. Statements
- Inside the
main
method, we write instructions. - Example:
System.out.println("Hello, World!");
6. Other Elements in Java Programs
Comments:
- Improve readability with
//
for single-line or/* ... */
for multi-line comments.
// Single-line comment
/* Multi-line
comment */
Variables and Data Types
- Store data values.
int number = 10;
Methods
- Functions inside a class.
public void greet() {
System.out.println("Welcome to Java!");
}
Objects
- Instances of a class used to call methods.
HelloWorld obj = new HelloWorld();
obj.greet();
✅ Example: A Simple Java Program
package myapp;
import java.util.Scanner;
public class StructureExample {
// method
public void greet(String name) {
System.out.println("Hello, " + name + "!");
}
// main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
// creating object
StructureExample obj = new StructureExample();
obj.greet(name);
}
}
Explanation:
public class StructureExample
: Defines a class named StructureExample. The file name must be HelloWorld.java.public static void main(String[] args)
: This is the entry point method where the program begins execution.System.out.println("Enter your name: ");
: This statement prints the text “Hello, World!” to the console.
📚 Steps to Compile and Run a Java Program
Step 1: Save the Program
Save your code to a file named StructureExample.java. In Java, the file name must always match the public class name.
Step 2: Open Terminal or Command Prompt
Navigate to the directory where the file is saved.
Step 3: Compile the Program
Run the following command to compile the Java file:
javac StructureExample.java
This will create a StructureExample.class file containing the bytecode.
Step 4: Run the Compiled Bytecode
Now, run the program with:
java StructureExample
Output:
Enter your name: Ashish
Hello, Ashish!
📝 Summary
In summary, the basic structure of a Java program includes a class definition to encapsulate data and behavior, and a main method that acts as the program’s entry point. Moreover, packages, imports, and comments improve maintainability and organization. Understanding this structure is essential for every beginner learning Java programming.
FAQ Section:
Q1: What is the main method in Java?
It is the entry point where execution begins: public static void main(String[] args)
.
Q2: Can a Java program run without a main method?
No, a standalone program requires a main method.
Q3: Why is class mandatory in Java?
Because Java is object-oriented; everything must reside inside a class.
Q4: Can we write multiple classes in one file?
Yes, but only one public class is allowed, and the filename must match it.