Lesson 1.8: Understanding Java Program Structure
Introduction
Every Java program follows a specific structure. Whether you are writing a simple “Hello, World!” program or a large enterprise application, the basic structure remains the same.
Understanding the structure of a Java program is essential because it helps you organize your code, avoid errors, and write programs that are easy to read and maintain.
In this lesson, you will learn the different parts of a Java program and understand the purpose of each component.
Learning Objectives
After completing this lesson, you will be able to:
-
Identify the different parts of a Java program.
-
Understand the purpose of packages, classes, methods, and statements.
-
Follow Java coding conventions.
-
Write well-structured Java programs.
Basic Structure of a Java Program
A typical Java program consists of the following components:
-
Comments (Optional)
-
Package Declaration (Optional)
-
Import Statements (Optional)
-
Class Declaration
-
Main Method
-
Program Statements
Let’s understand each part in detail.
Complete Example
// This is a simple Java program package demo; import java.util.Scanner; public class Student { public static void main(String[] args) { System.out.println("Welcome to Core Java"); } }
1. Comments
Comments are used to explain the code.
The Java compiler ignores comments.
They help programmers understand the purpose of the code.
Single-Line Comment
// This is a single-line comment
Multi-Line Comment
/* This is a multi-line comment. */
Documentation Comment
/** * This is a documentation comment. */
Documentation comments are used to generate API documentation.
2. Package Declaration
A package is used to organize Java classes into folders.
Example:
package demo;
If your program does not use packages, this line is not required.
3. Import Statement
The import keyword allows you to use predefined Java classes from other packages.
Example:
import java.util.Scanner;
This statement imports the Scanner class for taking user input.
Without importing it, you cannot use Scanner in your program.
4. Class Declaration
Every Java program must contain at least one class.
Example:
public class Student
Explanation
-
public → Access modifier
-
class → Keyword to define a class
-
Student → Class name
The class name should start with a capital letter according to Java naming conventions.
5. Main Method
public static void main(String[] args)
This is the entry point of every Java application.
The JVM starts program execution from this method.
There can be many methods in a class, but the JVM always looks for the main() method first.
6. Statements
Statements are instructions executed by the computer.
Example:
System.out.println("Welcome");
Every Java statement ends with a semicolon (;).
Curly Braces
Curly braces {} define blocks of code.
Example:
public class Demo { public static void main(String[] args) { System.out.println("Hello"); } }
Each opening brace { must have a matching closing brace }.
Java Naming Conventions
Following naming conventions makes your code professional and easy to read.
Class Names
-
Start with a capital letter.
-
Use PascalCase.
Examples:
Student
BankAccount
EmployeeDetails
Method Names
-
Start with a small letter.
-
Use camelCase.
Examples:
calculateSalary()
displayResult()
findMaximum()
Variable Names
-
Start with a small letter.
-
Use meaningful names.
Examples:
age
studentName
totalMarks
Constant Names
Constants are usually written in uppercase.
Examples:
PI
MAX_SIZE
MIN_AGE
Java Keywords
Keywords are reserved words that have special meanings in Java.
Some commonly used keywords are:
-
class
-
public
-
static
-
void
-
int
-
double
-
if
-
else
-
for
-
while
-
return
-
new
-
switch
You cannot use keywords as variable or class names.
Example (Invalid):
int class = 10;
Identifiers
Identifiers are names given to:
-
Variables
-
Methods
-
Classes
-
Objects
Example:
int age = 20;
Here, age is an identifier.
Rules for Identifiers
-
Must begin with a letter,
_, or$. -
Cannot start with a number.
-
Cannot contain spaces.
-
Cannot use Java keywords.
-
Java is case-sensitive.
Valid Examples:
student
studentName
_rollNo
$total
Invalid Examples:
2student
student name
class
Java is Case-Sensitive
Java treats uppercase and lowercase letters as different.
Example:
int age = 20; int Age = 30;
These are two different variables.
Best Coding Practices
-
Use meaningful names.
-
Write one statement per line.
-
Keep proper indentation.
-
Add comments where necessary.
-
Avoid unnecessary blank lines.
-
Follow Java naming conventions.
Key Points to Remember
-
Every Java program contains at least one class.
-
Execution starts from the
main()method. -
Statements end with a semicolon (
;). -
Curly braces define code blocks.
-
Java is case-sensitive.
-
Follow standard naming conventions for better readability.
Lesson Summary
In this lesson, you learned:
-
The structure of a Java program.
-
The purpose of comments, packages, imports, classes, and methods.
-
Java naming conventions.
-
Rules for identifiers.
-
The importance of writing clean and organized code.
Understanding the structure of a Java program will help you write professional and error-free code as you move on to more advanced topics.
Practice Questions
-
What are the main components of a Java program?
-
What is the purpose of the
main()method? -
Why do we use comments?
-
What is the difference between a package and an import statement?
-
What are Java identifiers?
-
Why is Java called a case-sensitive language?
-
Write three valid and three invalid identifiers.
-
What are Java naming conventions?
Programming Exercise
-
Create a class named
Student. -
Add a
main()method. -
Print your name and college name.
-
Add both single-line and multi-line comments.
-
Use proper indentation and naming conventions.
Assignment
Create a Java program named StudentInfo.java that:
-
Uses comments.
-
Declares a class named
StudentInfo. -
Contains the
main()method. -
Prints your Name, Roll Number, Department, and College Name.
-
Follows proper Java naming conventions and formatting.