Why Java’s structure matters (and why it’s easier than you think)

Imagine building a LEGO house: you need a base plate, walls, and a roof, otherwise the house falls apart. A Java program is the same – it has a few essential pieces that hold everything together.

💡 In Simple Words: A Java program is a set of instructions wrapped inside a class. The main method is the entry door where the computer starts reading your code. If you place these parts correctly, the program runs without a hitch.

What makes up a Java program?

Every .java file follows the same skeleton. Below is a quick rundown of each part.

  • Package statement – tells Java which folder (package) the class belongs to, like a mailing address.
  • Import statements – let you bring in ready‑made code (classes) from other packages, similar to borrowing a tool from a neighbor.
  • Class declaration – the blueprint that groups related code together. Think of it as the container for your house.
  • main method – the starting point of any standalone Java program. The computer looks for public static void main(String[] args) and begins execution there.
  • Statements – the actual actions (like printing text) that happen inside the main method.

Package and import – the “address” and “borrowed tools”

You can skip the package line for a simple program, but it’s good practice to include it when your project grows. An import line looks like import java.util.Scanner; – it lets you use the Scanner class for reading user input without writing it from scratch.

Class declaration – the container

Syntax: public class MyFirstProgram { … }. The word public means the class is visible to everyone, and class tells Java you’re defining a new type.

The main method – the entry door

The exact signature must be:

public static void main(String[] args) {

Breakdown:

  • public: anyone can call it.
  • static: you don’t need to create an object of the class to run it.
  • void: it doesn’t return any value.
  • String[] args: an array (list) of text values that can be passed from the command line.

Step‑by‑step: Write, compile, and run your first program

Let’s walk through a tiny “Hello, World!” example.

package com.example; // optional, shows where the class lives

import java.util.*; // we don’t actually need this now, but it’s handy later

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); // prints a line on the screen
    }
}

Save this as HelloWorld.java. Open a terminal, navigate to the folder, and type:

javac HelloWorld.java   // compiles the code into HelloWorld.class
java HelloWorld         // runs the compiled program

If everything is in order, you’ll see Hello, World! printed.

graph TD A[Write Java code] --> B[Save as .java file] B --> C[Compile with javac] C --> D[Run with java] D --> E[See output on screen]

Quick comparison table

PartPurposeTypical syntax
PackageOrganises classes into folderspackage mypackage;
ImportBrings external classes into scopeimport java.util.Scanner;
ClassDefines a blueprintpublic class MyClass { … }
main methodEntry point for executionpublic static void main(String[] args) { … }
StatementPerforms an actionSystem.out.println("Hi");

Common pitfalls and how to avoid them

  • Missing curly braces – every opening { needs a matching }. Think of them as doors; you can’t leave a door open.
  • Wrong main method signature – even a tiny typo like static public void main works, but public static void main() (without parameters) will not be recognized by the JVM.
  • File name mismatch – the file name must exactly match the public class name (case‑sensitive). If your class is HelloWorld, the file must be HelloWorld.java.
  • Using reserved words as identifiers – words like class, public, static are reserved; you can’t name a variable class.

📝 Likely Exam Questions

  1. Write a Java program that prints “Welcome to ICSE”. Include package and import statements.
  2. Explain the role of each keyword in the main method signature.
  3. What will happen if the file name does not match the public class name? Provide a brief answer.
  4. List three common errors beginners make when writing the structure of a Java program and how to fix them.
  5. Draw a flowchart showing the steps from writing code to seeing output on the screen. (You may refer to the flowchart above.)
#ICSE#Class 10#Computer Applications#Java#Programming Basics