←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Java Program Structure, Compilation & Execution, PATH and CLASSPATH

Study material and explanations for Unit 1

Java Program Structure, Compilation & Execution, PATH and CLASSPATH

1. Structure of a Java Program

A well-organized Java source file (.java) follows a standard structure divided into 6 sections. Out of these, only the Class Definition with main() method is mandatory for a standalone program.

Figure 1.6: Standard Structure of a Java Program
1Documentation Section
Optional — Comments (//, /*...*/, /**...*/) explaining author, date, and purpose
2Package Statement
Optional — Must be the FIRST non-comment line (e.g., package com.campusnotes;)
3Import Statements
Optional — Imports built-in or user packages (e.g., import java.util.Scanner;)
4Interface Section
Optional — Declares interfaces containing abstract methods and constants
5Class Definitions
Optional — Helper classes (e.g., class Student, class Account)
6Main Method Class
Essential — Contains public static void main(String[] args) where execution starts
  1. Documentation Section (Suggested): Contains comments to improve readability. Java supports three types of comments:
    • Single-line comment: // This is a single-line comment
    • Multi-line comment: /* This spans multiple lines */
    • Documentation comment: /** Used by javadoc tool to generate HTML API docs */
  2. Package Statement (Optional): Declares the folder/namespace to which the classes in this file belong (e.g., package college.bca;). If used, it must be the very first statement in the file (before any import).
  3. Import Statements (Optional): Similar to #include in C++, it instructs the compiler to load classes from other packages (e.g., import java.io.*; or import java.util.Scanner;).
    • Note: The java.lang package (which contains String, System, Math, Object, Thread) is imported automatically by default in every Java program!
  4. Interface Section (Optional): Used to declare interfaces for achieving abstraction and multiple inheritance.
  5. Class Definitions (Optional): A Java file can contain multiple helper classes that define data models and business logic.
  6. Main Method Class (Essential): Every standalone Java application must have at least one class containing the main() method, as it is the entry point from where the JVM starts program execution.

2. A Simple Java Program & Detailed Breakdown

Let us write a simple Java program, save it as HelloBCA.java, and understand every single word in it:

// 1. Documentation Section
/* File Name : HelloBCA.java
   Purpose   : First Simple Program in Java */

public class HelloBCA {
    // Entry point of the program
    public static void main(String[] args) {
        System.out.println("Welcome to Java Programming - BCA DAVV!");
    }
}

Output:

Welcome to Java Programming - BCA DAVV!

Meaning of Every Keyword in public static void main(String[] args)

This is the #1 Viva and Short-Note Question in BCA Java exams:

  1. public (Access Specifier): main() must be declared public so that the JVM (which sits outside your class) can access and invoke it from anywhere.
  2. static (Keyword): When your program starts, no objects of the class exist yet. Declaring main() as static allows the JVM to call HelloBCA.main() directly using the class name, without creating an object of HelloBCA first.
  3. void (Return Type): Indicates that the main() method does not return any value back to the JVM when it finishes.
  4. main (Method Name): The fixed identifier name that the JVM looks for as the starting point of execution.
  5. String[] args (Command-Line Arguments): An array of String objects that receives any arguments passed by the user from the command prompt when launching the program (e.g., java HelloBCA Indore 2026).

Meaning of System.out.println(…)

  • System: A predefined final class inside the java.lang package (note the capital S).
  • out: A static reference variable of type PrintStream declared inside the System class, representing the standard output stream (your monitor screen).
  • println(): A method of the PrintStream class that prints the text to the console and moves the cursor to a new line.

3. Compilation and Execution of a Java Program

Golden Rule of Naming a .java File

  • If a class is declared as public (e.g., public class HelloBCA), the source file MUST be saved with the exact same name, including uppercase/lowercase letters: HelloBCA.java.
  • Why? Because there can be at most one public class per .java file, and the compiler uses that filename to locate the public class.

Step-by-Step Terminal Commands

  1. Save the File: Save the code in a file named HelloBCA.java.
  2. Open Command Prompt / Terminal: Navigate to the folder where HelloBCA.java is saved.
  3. Compile the Program (javac):
    javac HelloBCA.java
    • The Java compiler (javac) checks for syntax errors. If there are no errors, it generates a bytecode file named HelloBCA.class in the same directory.
  4. Execute the Program (java):
    java HelloBCA
    • Notice: Do not write .class or .java with the java command! You pass the Class Name (HelloBCA) to the JVM, which loads HelloBCA.class and calls its main() method.

4. The Concept of PATH and CLASSPATH

When working with Java from the command line, two environment variables are crucial: PATH and CLASSPATH. Students often confuse them, making this a favorite exam question!

1. What is PATH?

  • Purpose: PATH is an Operating System (OS) environment variable that tells the OS (Windows/Linux) where to find executable tool files (.exe binaries) like javac.exe (compiler) and java.exe (launcher).
  • What happens if PATH is not set? When you type javac HelloBCA.java in Command Prompt, Windows will throw the error: 'javac' is not recognized as an internal or external command, operable program or batch file.
  • How to set PATH ( points to JDK’s bin directory):
    set PATH=C:\Program Files\Java\jdk-21\bin;%PATH%

2. What is CLASSPATH?

  • Purpose: CLASSPATH is a Java-specific environment variable used by the Java Compiler (javac) and the JVM (java) to locate compiled .class files (Bytecode) and external library .jar files (like a MySQL JDBC driver) needed by your program.
  • What happens if CLASSPATH is not set properly? The JVM will throw a runtime error: Error: Could not find or load main class HelloBCA or java.lang.ClassNotFoundException.
  • How to set CLASSPATH (points to directories containing .class or .jar files):
    set CLASSPATH=.;C:\myproject\classes;C:\libs\mysql-connector.jar
    (Note: The dot . represents the current working directory).

5. Comparison: PATH vs CLASSPATH

Feature PATH CLASSPATH
1. Used By Used by the Operating System (Windows, Linux, macOS). Used by the Java Compiler (javac) and JVM (java).
2. What It Locates Locates native JDK executable tools (.exe files) such as javac.exe, java.exe, javadoc.exe, jar.exe. Locates user-defined and library .class files (Bytecode) and .jar archives.
3. Target Directory Points to the jdk\bin folder. Points to folders containing .class files or .jar library paths.
4. Error If Missing 'javac' is not recognized as an internal or external command. NoClassDefFoundError or ClassNotFoundException.
5. Can Be Overridden via CLI? Cannot be overridden by a java flag. Can be easily overridden per command using java -cp or java -classpath.

💡 Exam Preparation Tips for DAVV BCA

  • Always remember the 1-line summary for exams:
    • PATH points to jdk/bin to find .exe tools (javac, java).
    • CLASSPATH points to project/library folders to find .class and .jar files.
  • When explaining public static void main(String[] args), explain each of the 5 tokens (public, static, void, main, String[] args) in separate bullet points.