Skip to content
Computer ApplicationsICSE Class 9 · Basic Structure of a Class in Java

Class Header, main() Method, Comments and Layout Rules of a Java Program (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

A basic Java program is a class (comment, class header, braces) holding the method main(), where execution begins, with statements ending in semicolons. By convention a class name starts with a capital letter and the file is named after the class with .java. Java is case sensitive, braces must pair up, and comments (// or /* */) are ignored by the compiler. The CISCE syllabus treats public, static and String args[] as default.

Understand Class Header, main() Method, Comments and Layout Rules of a Java Program

The structure.
// comment (not executed)
class ClassName
{
public static void main(String args[])
{
statements;
}
}

Parts.
1. Comment: a note for readers; the compiler ignores it. // runs to the end of the line; /* ... */ may cover many lines.
2. Class header: the keyword class and a class name (letters, digits, underscore; not starting with a digit; no spaces; not a keyword). By convention the first letter is a capital: Triangle, FareCalc.
3. Braces { }: the class has a pair, and main() has a pair inside it. Every opening brace needs its closing brace; indent the inner lines.
4. main(): the entry point. In the full header, public lets the JVM call it, static lets it run without an object, void means it returns nothing, and String args[] can receive command-line values. The syllabus calls public, static and String args[] default, and BlueJ lets you write void main().
5. Statements: instructions inside main(). Each ends with a semicolon, but a header line (class or main) does not.

Rules of layout.

  • Java is case sensitive: System, String and Scanner start with capitals; main, class, int and import are in small letters. Main() is not main().
  • The file name should be the class name plus .java (Triangle.java). A class declared public must be in a file with exactly that name.
  • Variables must be declared before they are used.
  • One statement per line keeps the program readable.

What the compiler reports (typical messages). A missing semicolon gives ';' expected. A misspelt keyword such as Int gives an error about the unknown symbol. An unclosed brace gives "reached end of file while parsing". An unclosed multi-line comment gives "unclosed comment". Such mistakes are syntax errors and the program will not compile until they are fixed.

Printing in the structure. System.out.print() keeps the cursor on the same line; System.out.println() moves to the next line after printing.

Neighbouring topics: programs that read values with the Scanner class are on the next page of this chapter.

Class Header, main() Method, Comments and Layout Rules of a Java Program: key facts

Skeleton
  • comment
  • class Name {
  • main() header {
  • statements;
  • }
  • }
Statements end with a semicolon; headers do not.
Full main header
public static void main(String args[])
publicJVM can call it
staticno object needed
voidreturns nothing
BlueJ accepts void main().
Naming rule
  • Class name: letters, digits, underscore
  • not starting with a digit
  • no spaces
  • not a keyword
Start with a capital by convention.
File name
ClassName.java | must match a public class exactly
Triangle.java for class Triangle.

Class Header, main() Method, Comments and Layout Rules of a Java Program in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Fixed form

Suppose a railway ticket must show the train number, date, coach and seat in a fixed layout so the machine can read it.

How is a Java program like this ticket?

Try it first, then show the solutionSolution
  1. The ticket has parts in a fixed order.
  2. A Java class has comment, class header, main() and statements in a fixed order.
  3. If a part is missing, the compiler (like the ticket machine) rejects it.

Answer: A Java program must follow a fixed structure, otherwise the compiler reports errors.

Memory hook: Format first, content next.

Braces like brackets in a sum

Imagine writing (2 + (3 x 4) and forgetting a closing bracket; the sum cannot be finished.

What similar thing happens if a closing brace is forgotten in Java?

Try it first, then show the solutionSolution
  1. Each opening brace needs a matching closing brace.
  2. The compiler reaches the end of the file while still waiting for a brace.
  3. It reports an error and does not compile.

Answer: The compiler reports that it reached the end of the file while parsing, and the program does not compile.

Memory hook: Every { needs its }.

Tricks to remember Class Header, main() Method, Comments and Layout Rules of a Java Program

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Capital for classes

    Class names start with a capital, variable names with a small letter.

    Use it when: Naming conventions.

  • Headers have no semicolon

    Statements end with ; but class and main() headers do not.

    Use it when: Syntax errors.

  • Same name, same case

    File name = class name, letter for letter.

    Use it when: Saving a program.

Class Header, main() Method, Comments and Layout Rules of a Java Program: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Valid class name · 1 mark · MCQBoard level

Which is a valid class header?
(a) class 1Test (b) class Test1 (c) Class Test (d) class test one

Try it first, then show the solutionSolution
  1. (a) starts with a digit; (c) has Class in capitals; (d) contains a space.
  2. (b) Test1 is a valid name.

Answer: (b) class Test1

Example 2: Find and correct the errors · 3 marks · Short answerHarder board level

Find three errors:

class Tally
{
public static void main(String args[])
{
int x = 8, y = 2
Int p = x * y;
System.out.println("Product = " + p);
}
Try it first, then show the solutionSolution
  1. A semicolon is missing after int x = 8, y = 2. (1 mark)
  2. Int is wrong because Java is case sensitive: it must be int. (1 mark)
  3. The closing brace of the class is missing (one more } is needed at the end). (1 mark)

Answer: (1) add ; after y = 2 (2) write int instead of Int (3) add the closing } of the class

Example 3: print, println and comments · 3 marks · Short answerHarder board level

Give the output:

// totals
int a = 12, b = 5;
System.out.print(a + b);
System.out.print(" ");
System.out.println(a - b);
System.out.println(a * b);
/* System.out.println("hidden"); */
Try it first, then show the solutionSolution
  1. a + b = 17 is printed, then a space, then a - b = 7 and the cursor moves to a new line. (1 mark)
  2. a * b = 60 is printed on the next line. (1 mark)
  3. The multi-line comment is not executed, so "hidden" is never printed. (1 mark)

Answer: 17 7
60

Example 4: Case study: a fare program · 4 marks · Case-basedHOTS / competency

A student saves this program as Fare.java:

import java.util.*;
class Fare
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int adults, children;
System.out.print("Adults: ");
adults = sc.nextInt();
System.out.print("Children: ");
children = sc.nextInt();
int total = adults * 150 + children * 80;
System.out.println("Total fare = Rs " + total);
}
}

(i) Name the statement that creates the Scanner object. (1)
(ii) For the input 3 and 2 (each followed by Enter), state the total fare. (2)
(iii) What error appears if the import line is deleted? (1)

Try it first, then show the solutionSolution
  1. (i) Scanner sc = new Scanner(System.in);
  2. (ii) 3 x 150 = 450 and 2 x 80 = 160, so total = 610. The screen shows Adults: 3, Children: 2 and Total fare = Rs 610 on three lines.
  3. (iii) The compiler cannot find the symbol Scanner, because java.util is not imported.

Answer: (i) Scanner sc = new Scanner(System.in); (ii) Rs 610 (iii) cannot find symbol Scanner

Example 5: Order of the parts · 3 marks · Short answerHarder board level

A program with keyboard input has these parts in a jumbled order: (a) set of statements; (b) declaration of variables; (c) import java.util.*; (d) creating the Scanner object; (e) accepting values; (f) class header. Write the correct order.

Try it first, then show the solutionSolution
  1. Import comes first, then the class header. (1 mark)
  2. Inside main(): declaration of variables, then the Scanner object. (1 mark)
  3. Then accepting values, and last the set of statements. (1 mark)

Answer: (c), (f), (b), (d), (e), (a)

Where students lose marks in Class Header, main() Method, Comments and Layout Rules of a Java Program

The slips examiners see most often, and what to write instead.

  • Mistake: Semicolon after a header

    Statements and headers are confused.

    Wrong working

    class Demo;

    Right working

    class Demo

    Do this: Only statements end with a semicolon.

    Usually costs: 1 mark

  • Mistake: Wrong letter case

    Java is case sensitive.

    Wrong working

    system.out.println("Hi");

    Right working

    System.out.println("Hi");

    Do this: System and String start with capital letters.

    Usually costs: 1 mark

  • Mistake: Unbalanced braces

    The inner braces are not counted.

    Wrong working

    Closing brace of the class missing.

    Right working

    Every { has a matching }.

    Do this: Indent and count the pairs.

    Usually costs: 1 to 2 marks

Exam tips

  • Always write the class, main() and braces fully in program answers.
  • Add short comments for the declaration, input, processing and output sections.
  • Quote the compiler message for syntax-error questions when you know it.
  • Use the full main() header unless the question allows void main().

Practice questions on Class Header, main() Method, Comments and Layout Rules of a Java Program

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    A class named Triangle should be saved in a file named:
    (a) triangle.txt (b) Triangle.class (c) Triangle.java (d) Triangle.exe

    Hint

    Source files have the .java extension.

    Answer

    (c) Triangle.java

  2. Q2 · 1 mark · Very short answerBoard level

    What happens if import java.util.*; is left out of a program that uses Scanner?

    Hint

    Think of the compiler's message.

    Answer

    The compiler cannot find the symbol Scanner and the program does not compile.

  3. Q3 · 4 marks · Short answerHarder board level

    Write a complete program (no input) that stores length 9 and breadth 4 of a rectangle in int variables and prints the area and the perimeter on two lines. State the output.

    Hint

    Area = l * b; perimeter = 2 * (l + b).

    Answer

    class Rect
    {
    public static void main(String args[])
    {
    int l = 9, b = 4;
    System.out.println("Area = " + (l * b));
    System.out.println("Perimeter = " + 2 * (l + b));
    }
    }
    Output:
    Area = 36
    Perimeter = 26

  4. Q4 · 2 marks · Short answerHarder board level

    A student writes:

    /* find the sum
    int a = 4;
    int b = 6;
    System.out.println(a + b);

    and gets a compile error. Explain the error and correct it.

    Hint

    How does a multi-line comment end?

    Answer

    The comment starts with /* but is never closed with */, so the compiler reports an unclosed comment (and everything after /* is treated as comment). Add */ at the end of the first line.

  5. Q5 · 2 marks · Short answerHOTS / competency

    A student saves the file as Demo.java but declares public class Test in it. Will it compile? Explain.

    Hint

    Which name must a public class match?

    Answer

    No. A public class must be saved in a file with exactly the same name, so Test must be in Test.java; the compiler reports an error for Demo.java.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Where does execution of a Java program begin?Show answer

    At main().

  2. 2.Which symbol ends a Java statement?Show answer

    A semicolon.

  3. 3.Is Main() the same as main()?Show answer

    No. Java is case sensitive.

  4. 4.Symbol for a single-line comment?Show answer

    //

  5. 5.Symbol pair for a multi-line comment?Show answer

    /* and */

  6. 6.Extension of a Java source file?Show answer

    .java

Class Header, main() Method, Comments and Layout Rules of a Java Program: frequently asked questions

Do I write public static in BlueJ?

The syllabus treats them as default, and BlueJ runs void main(). The full header is never wrong.

Must the class name begin with a capital letter?

It is a convention that is expected in answers, not a compiler rule.

Do comments slow the program?

No. The compiler ignores them.