Basic Structure of a Class in Java ICSE Class 9 Computer Applications
Updated 15 October 2026 · Fact-checked
A basic Java program is a class containing a main() method: an optional comment, the class header, braces, and inside main() the declarations and statements. With input, it starts with import java.util.*; creates a Scanner object and reads values with its methods. ICSE Class 9 treats public, static and String args[] in the main() header as default.
What Basic Structure of a Class in Java is about
Every program you write in ICSE follows one standard shape. Once the shape is automatic, you can concentrate on the logic. CISCE prescribes two versions of the structure: one without input and one that accepts input with the Scanner class.
The CISCE syllabus gives the structure as: // comment [non-executable]; class <name of the class> { void main(<parameters>) { set of statements } }; and, with the Scanner class: import java.util.*; class <name of the class> { void main() { declaration of variables; create an object of the Scanner class; accept the required values using the methods of the Scanner class; set of statements } }. It states that the keywords public, static and String args[] are default in nature, and that the same structure can be followed for designing any other program.
In BlueJ, void main() can be run directly. Older Java versions, such as the JDK 17 used to test this guide, need public static void main(String args[]) to run a program from the command line (recent versions have begun to accept a shorter main()). This guide uses the full form, which works on every version.
Weightage: CISCE publishes no chapter weightage and no question-paper layout for ICSE Class 9 Computer Applications. Class IX is examined by your school: one 100-mark theory paper of 2 hours, plus 100 marks of internal assessment (at least 15 programming assignments and a written project on ethical computing). This is topic 5 of the 2028 syllabus.Source: CISCE ICSE 2028 Computer Applications syllabus, Class IX
Key concepts
- The structure without input
// comment (non-executable: ignored by the compiler)
class ClassName
{
void main() // BlueJ form; full form: public static void main(String args[])
{
set of statements;
}
}Parts:
1. Comment: describes the program for human readers; not executed.
2. Class header: the keyword class followed by the class name. By convention the name starts with a capital letter (Greeting, Rectangle). The class name should match the file name (Greeting.java).
3. Opening and closing braces { } of the class: everything in the program is inside them.
4. main() method: the starting point of execution. Its body is enclosed in its own braces.
5. Statements: instructions inside main(), each ending with a semicolon.As run in this study guide:
// A class without input
class Greeting
{
public static void main(String args[])
{
String name = "Kabir";
int year = 2028;
System.out.println("Hello, " + name);
System.out.println("ICSE exam year: " + year);
}
}
Output:
Hello, Kabir
ICSE exam year: 2028Worked example
(2 marks) Why is main() important in a Java program? What happens if it is misspelt as Main()?
Try it first, then show the solutionSolution
- main() is the entry point: execution begins from its first statement - 1 mark.
- Java is case sensitive; Main() is a different method, so the program compiles but cannot be run as a program (the JVM reports that the main method was not found) - 1 mark.
Answer: main() is where execution starts. Because Java is case sensitive, a method named Main() is not recognised as the entry point: the class compiles, but running it gives an error that the main method was not found.
- public, static and String args[]
The full header is: public static void main(String args[])
- public: an access specifier that lets the JVM (outside the class) call main().
- static: main() belongs to the class itself, so it can run without creating an object first.
- void: main() does not return a value.
- main: the fixed name of the starting method.
- String args[]: an array of strings that can receive values typed on the command line when the program starts (command-line arguments).
The CISCE syllabus says public, static and String args[] are 'default in nature': in BlueJ you may write simply void main() and BlueJ supplies what is needed when you run it. Many other tools, and the command line in older Java versions such as JDK 17, need the full header. Writing the full header is never wrong.
Worked example
(2 marks) State the purpose of the keywords public and static in the main() header.
Try it first, then show the solutionSolution
- public: makes main() accessible from outside the class, so the JVM can call it - 1 mark.
- static: lets main() run without creating an object of the class - 1 mark.
Answer: public allows main() to be called from outside the class, by the JVM. static means main() belongs to the class and can be called without first creating an object.
- The structure with the Scanner class
import java.util.*;
class ClassName
{
void main()
{
declaration of variables;
create an object of the Scanner class;
accept the required values using the methods of the Scanner class;
set of statements;
}
}Step by step:
1. import java.util.*; makes the classes of the package java.util, including Scanner, available. Without it the compiler cannot find the Scanner class (unless you write java.util.Scanner in full).
2. Declare variables: int l, b, area, peri;
3. Create a Scanner object linked to the keyboard: Scanner sc = new Scanner(System.in);
4. Prompt and accept values: System.out.print("Enter length: "); l = sc.nextInt();
5. Process and output: area = l * b; System.out.println("Area = " + area);import java.util.Scanner; imports only the Scanner class; import java.util.*; imports all classes of the package. Either works.
Worked example
(2 marks) What is the purpose of (i) import java.util.*; (ii) Scanner sc = new Scanner(System.in);?
Try it first, then show the solutionSolution
- (i) Makes all classes of java.util, including Scanner, available to the program - 1 mark.
- (ii) Creates a Scanner object sc that reads input from the keyboard (System.in) - 1 mark.
Answer: (i) It imports the java.util package so that its classes, such as Scanner, can be used. (ii) It creates an object sc of the Scanner class connected to the standard input (keyboard), so values can be read with methods like sc.nextInt().
- A complete program following the structure
// Program to find the area and perimeter of a rectangle
import java.util.*;
class Rectangle
{
public static void main(String args[])
{
int l, b, area, peri; // declaration of variables
Scanner sc = new Scanner(System.in); // object of the Scanner class
System.out.print("Enter length: ");
l = sc.nextInt(); // accept values
System.out.print("Enter breadth: ");
b = sc.nextInt();
area = l * b; // set of statements
peri = 2 * (l + b);
System.out.println("Area = " + area);
System.out.println("Perimeter = " + peri);
}
}Sample run (as run in this study guide, inputs 12 and 8):
Enter length: 12
Enter breadth: 8
Area = 96
Perimeter = 40Every part of the prescribed structure is labelled by a comment. Use this layout for every program: it earns the structure marks even if the logic has a slip.
Worked example
(2 marks) In the Rectangle program, why is print() used for the prompts and println() for the results?
Try it first, then show the solutionSolution
- print() keeps the cursor on the same line, so the user types the value next to the prompt - 1 mark.
- println() moves to a new line after each result, so the results appear on separate lines - 1 mark.
Answer: print() leaves the cursor on the prompt line, so the input is typed beside 'Enter length:'. println() ends each result line, so Area and Perimeter appear on separate lines.
- Rules of layout: braces, semicolons, comments and case
1. Every statement ends with a semicolon (;). Headers of the class and of main() do not.
2. Braces come in pairs: the class's { } contain main(); main()'s { } contain the statements. Indent the inner lines so the pairs are easy to match.
3. Java is case sensitive: System, String and Scanner start with capitals; main, class, int, import are lower case.
4. Comments: // for a single line; /* ... */ for several lines. They explain the program and are ignored by the compiler.
5. The file name should be the same as the class name, with .java (Rectangle.java). If the class is declared public, it must match.
6. Declarations come before the variables are used.What the compiler says (as checked in this study guide):
- Missing semicolon after int n = sc.nextInt() gives the error: ';' expected.
- Without import java.util.*; the compiler reports that it cannot find the symbol Scanner.
- Writing Main instead of main compiles, but running gives: Main method not found in class.
Worked example
(2 marks) Find two errors: class Demo { public static void main(String args[]) { int n = 5 System.Out.println(n); } }
Try it first, then show the solutionSolution
- Missing semicolon after int n = 5 - 1 mark.
- System.Out should be System.out (Java is case sensitive) - 1 mark.
Answer: (1) A semicolon is missing after int n = 5. (2) Out must be written in lower case: System.out.println(n);
Basic Structure of a Class in Java in real life
Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Fixed structure
Suppose a school asks every student to submit a letter in a fixed format: sender's address, date, salutation, body, closing.
How is a Java program's structure similar?
Try it first, then show the solutionSolution
- A letter has fixed parts in a fixed order.
- A program has comment, import, class header, main() header, declarations, input, processing, output in order.
- Following the format makes it readable and accepted (compiled).
Answer: Like a formal letter, a Java program has a prescribed order: comment, import, class, main(), declarations, Scanner object, input, processing and output. Following it makes the program clear and correct.
Memory hook: Format first, content next.
import
Imagine you need a drill for a school project and borrow it from a neighbour's toolbox instead of building one yourself.
How does import java.util.*; resemble this?
Try it first, then show the solutionSolution
- Java keeps ready-made classes in packages (toolboxes).
- import makes a package's classes available to your program.
- You can then use Scanner without writing it yourself.
Answer: import borrows ready-made tools: java.util.* is a toolbox containing Scanner, so the program can use it directly.
Memory hook: import = borrow the toolbox.
Prompt and input
Suppose a ticket machine at a metro station shows 'Enter destination' and waits for you to press a button.
Which two statements in a Java program play the same roles?
Try it first, then show the solutionSolution
- The message on the screen is a prompt: System.out.print("Enter length: ");
- Waiting for and reading your choice is input: l = sc.nextInt();
Answer: The prompt is printed with System.out.print(), and the value is read with a Scanner method such as sc.nextInt().
Memory hook: Ask, then accept.
Topics in this chapter
Basic Structure of a Class in Java: syntax and key facts
- Structure without input
- // comment → class Name { void main() { statements } }
- Full header: public static void main(String args[])
- Structure with input
- 1.import java.util.*;
- 2.class
- 3.main()
- 4.declare
- 5.Scanner sc = new Scanner(System.in);
- 6.accept
- 7.process
- 8.output
- Prescribed order.
- Syntax rules
- ; ends statements; { } in pairs; case sensitive; // and /* */ comments
- File name = class name + .java
How the school exam asks Basic Structure of a Class in Java
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| MCQ (1 mark) | Which statement imports Scanner? Which keyword makes main() callable without an object? Comment symbols. | import java.util.*; static; // and /* */. |
| Short answer (2 marks) | Purpose of public, static, String args[]; why main() is needed; role of import and the Scanner object. | One clear line per keyword. |
| Error spotting (2 marks) | Find and correct errors in a short program. | Check semicolons, braces, case, import and the main() header. |
| Program (15 marks) | Any program accepting values with Scanner and printing results. | Follow the prescribed structure; comment each part. |
Tricks to remember Basic Structure of a Class in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
I-C-M-D-S-A-P-O
Import, Class, Main, Declare, Scanner, Accept, Process, Output.
Use it when: Order of a program with input.
PSVM
public static void main: 'Please Start Very Merrily'.
Use it when: Full main() header.
Capital S, small m
System, String, Scanner start with capitals; main, class, int, import are small.
Use it when: Case-sensitivity errors.
Basic Structure of a Class in Java: exam-level solved questions
Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Import statement · 1 mark · MCQExam level
Which statement must appear before the class to use the Scanner class?
(a) import java.io.*;
(b) import java.util.*;
(c) import java.lang.*;
(d) import Scanner;
Try it first, then show the solutionSolution
- Scanner belongs to the package java.util.
- So import java.util.*; (or import java.util.Scanner;).
Answer: (b) import java.util.*;
Example 2: Assertion and reason: static · 1 mark · Assertion-reasonHarder exam level
Assertion (A): The JVM can run main() without creating an object of the class.
Reason (R): main() is declared static.
(a) Both A and R are true and R is the correct explanation of A
(b) Both A and R are true but R is not the correct explanation of A
(c) A is true but R is false
(d) A is false but R is true
Try it first, then show the solutionSolution
- A static method belongs to the class, not to an object.
- So it can be called directly; R explains A.
Answer: (a) Both A and R are true and R is the correct explanation of A
Example 3: Spot the errors · 2 marks · Short answerHarder exam level
Find and correct three errors:
class Average
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt()
int b = sc.nextInt();
system.out.println((a + b) / 2.0);
}
}
Try it first, then show the solutionSolution
- import java.util.*; is missing before the class; a semicolon is missing after sc.nextInt().
- system must be System (capital S).
Answer: (1) Add import java.util.*; before class Average. (2) int a = sc.nextInt(); needs a semicolon. (3) Write System.out.println((a + b) / 2.0);
Example 4: Explain the main() header · 2 marks · Very short answerExam level
Explain each word of the header public static void main(String args[]).
Try it first, then show the solutionSolution
- public: callable from outside the class (by the JVM); static: runs without an object.
- void: returns nothing; main: the entry point; String args[]: receives command-line arguments.
Answer: public lets the JVM call it; static lets it run without creating an object; void means it returns no value; main is the name of the starting method; String args[] can receive command-line arguments.
Example 5: Decimal surprise · 2 marks · Short answerHOTS / competency
The Temperature program computes k = c + 273.15. With input 37.5 it prints 310.65, but with input −40 it prints 233.14999999999998. Why?
Try it first, then show the solutionSolution
- double values are stored in binary, and many decimals such as 273.15 cannot be represented exactly.
- The tiny representation error shows up in some results (−40 + 273.15), not in others; the value is still correct to many decimal places.
Answer: A double stores numbers in binary, so 273.15 is held as a very close approximation. Adding it to −40.0 exposes the tiny error (233.14999999999998), while 37.5 + 273.15 happens to round to 310.65. Both results are accurate for practical use; rounding can be applied when printing.
Example 6: Comments · 2 marks · Very short answerHarder exam level
Write a single-line and a multi-line comment for a program that converts Celsius to Fahrenheit. Are comments executed?
Try it first, then show the solutionSolution
- // Converts Celsius to Fahrenheit
- /* Program to convert a temperature from Celsius
to Fahrenheit and Kelvin */ Comments are not executed; the compiler ignores them.
Answer: // Converts Celsius to Fahrenheit
/* Program to convert a temperature from Celsius
to Fahrenheit and Kelvin */
Comments are non-executable: the compiler ignores them.
Example 7: Temperature conversion program · 15 marks · Long answerHarder exam level
Following the prescribed structure, write a program to accept a temperature in Celsius and print it in Fahrenheit (F = C × 9/5 + 32) and Kelvin (K = C + 273.15).
Try it first, then show the solutionSolution
- /* Program to convert a temperature from Celsius
to Fahrenheit and Kelvin */
import java.util.*;
class Temperature
{
public static void main(String args[])
{ - double c, f, k;
Scanner sc = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
c = sc.nextDouble(); - f = c * 9 / 5 + 32; // Fahrenheit
k = c + 273.15; // Kelvin - System.out.println("Celsius : " + c);
System.out.println("Fahrenheit : " + f);
System.out.println("Kelvin : " + k);
}
} - c is double, so c * 9 / 5 is a decimal calculation (writing 9 / 5 first with ints would give 1). Trace 37.5: 37.5 × 9 = 337.5; ÷ 5 = 67.5; + 32 = 99.5; 37.5 + 273.15 = 310.65.
Answer: Sample run (as run, input 37.5):
Enter temperature in Celsius: 37.5
Celsius : 37.5
Fahrenheit : 99.5
Kelvin : 310.65
Case-study practice (Section B style)
Read the situation, then answer every part. In a school exam set on the board pattern this is a 6-mark question split into 2 + 2 + 2 marks.
A program that will not run
Imagine Tara types this in a plain editor and compiles it from the command line with JDK 17:
class fees
{
void main()
{
int months = 12; int fee = 1500;
System.out.println("Annual fee: " + months * fee);
}
}
It compiles, but the command java fees reports that no main method can be found. In BlueJ, the same class runs.
(i) · 2 marks
Why does the program run in BlueJ but not from the command line?
Answer
Answer: BlueJ can call void main() on an object directly, treating public and static as default. The JDK 17 launcher needs public static void main(String args[]) as the entry point (some newer Java versions relax this, but the full header works everywhere).
1 mark BlueJ default, 1 mark full header needed.
(ii) · 2 marks
Rewrite the main() header so it runs everywhere, and suggest a better class name.
Answer
Answer: public static void main(String args[]); class name Fees (capital letter by convention), saved as Fees.java.
1 mark each.
(iii) · 2 marks
What is printed? Explain how the expression is evaluated.
Answer
Answer: Annual fee: 18000. In "Annual fee: " + months * fee, * has higher precedence than +, so 12 * 1500 = 18000 is computed first and then joined to the text.
1 mark output, 1 mark precedence.
Where the marks are
How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.
State the purpose of import java.util.*; and of Scanner sc = new Scanner(System.in);. (2 marks) (2 marks)
| Step | Marks |
|---|---|
| import makes the java.util classes, including Scanner, available. | 1 |
| Creates Scanner object sc reading from the keyboard (System.in). | 1 |
Program: accept Celsius and print Fahrenheit and Kelvin. (15 marks) (15 marks)
| Step | Marks |
|---|---|
| Comment, import and class header. | 2 |
| main() header and variable declarations. | 2 |
| Scanner object, prompt and nextDouble() input. | 4 |
| Correct formulas for Fahrenheit and Kelvin (decimal arithmetic). | 4 |
| Labelled output of all three values. | 3 |
Where students lose marks in Basic Structure of a Class in Java
The slips examiners see most often, and what to write instead.
Mistake: Forgetting import java.util.*;
The class seems complete without it.
Do this: Write the import line first in every program that uses Scanner.
Usually costs: 1 to 2 marks.
Mistake: Wrong case in System, String or Scanner
Typing fast.
Wrong working
system.out.println("Hi");
Right working
System.out.println("Hi");
Do this: Class names start with capitals; check every line.
Usually costs: 1 mark per error.
Mistake: Unmatched braces
No indentation.
Do this: Indent the body of main() inside the class; count opening and closing braces.
Usually costs: 1 to 2 marks.
Mistake: Integer formula for a decimal answer
Writing 9 / 5 with int operands.
Wrong working
f = 9 / 5 * c + 32; (9 / 5 = 1)
Right working
f = c * 9 / 5 + 32; or f = 9.0 / 5 * c + 32;
Do this: Make sure a double takes part before any division.
Usually costs: 2 to 3 marks.
Practice questions on Basic Structure of a Class in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQExam level
A single-line comment begins with:
(a) /*
(b) //
(c) **
(d) #Hint
Two slashes.
Answer
(b) //
Q2 · 1 mark · MCQExam level
The method from which execution of a Java application begins is:
(a) start()
(b) init()
(c) main()
(d) run()Hint
Entry point.
Answer
(c) main()
Q3 · 1 mark · Very short answerExam level
Write the statement to create a Scanner object named in that reads from the keyboard.
Hint
System.in
Answer
Scanner in = new Scanner(System.in);
Q4 · 2 marks · Very short answerHarder exam level
What does String args[] in the main() header hold?
Hint
Command line.
Answer
An array of String values (command-line arguments) that can be passed to the program when it is started; ICSE programs usually leave it unused.
Q5 · 2 marks · Short answerHOTS / competency
Give the output: class T { public static void main(String args[]) { int months = 12, fee = 1500; System.out.println("Annual fee: " + months * fee); System.out.println("Sum: " + months + fee); } }
Hint
Precedence of * over +.
Answer
Annual fee: 18000
Sum: 121500Q6 · 2 marks · Very short answerHarder exam level
Differentiate between import java.util.*; and import java.util.Scanner;
Hint
All vs one.
Answer
import java.util.*; makes all classes of java.util available; import java.util.Scanner; makes only the Scanner class available. Both allow Scanner to be used.
Q7 · 15 marks · Long answerHarder exam level
Write a program, following the prescribed structure, to accept the price of an item and the quantity bought and print the total amount.
Hint
double price, int qty.
Answer
// Bill for one item
import java.util.*;
class Bill
{
public static void main(String args[])
{
double price, total;
int qty;
Scanner sc = new Scanner(System.in);
System.out.print("Enter price: ");
price = sc.nextDouble();
System.out.print("Enter quantity: ");
qty = sc.nextInt();
total = price * qty;
System.out.println("Total amount = " + total);
}
}
Sample run (as run, inputs 45.5 and 4):
Enter price: 45.5
Enter quantity: 4
Total amount = 182.0Q8 · 2 marks · Very short answerHarder exam level
A student puts the declaration int area; after the statement area = l * b; inside main(). What happens, and why?
Hint
Declare before use.
Answer
A compile-time error (cannot find symbol area), because a variable must be declared before it is used; statements in main() are processed in order.
Q9 · 2 marks · Short answerHOTS / competency
Why is it good practice to print a prompt before every input statement? What could go wrong without it?
Hint
The user cannot see what is expected.
Answer
Without a prompt the program waits silently, and the user does not know what to type or in what order; they may enter breadth before length or a decimal where an int is expected, causing wrong results or an input error.
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.Package that contains Scanner?Show answer
java.util
2.What is System.in?Show answer
The standard input stream (keyboard).
3.Which keywords does CISCE treat as default in main()?Show answer
public, static and String args[].
4.Multi-line comment symbols?Show answer
/* and */
5.What ends a Java statement?Show answer
A semicolon (;).
Best order to study it
- Learn the structure without input and write it from memory.
- Learn the meaning of public, static, void, main and String args[].
- Learn the structure with Scanner and the role of import.
- Practise complete programs: rectangle, temperature, bill.
- Practise spotting errors: semicolons, braces, case, missing import.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- // comment; class Name { void main() { statements } }
- With input: import java.util.*; → class → main() → declare → Scanner sc = new Scanner(System.in); → accept → process → output.
- public: JVM can call; static: no object needed; void: no return; String args[]: command-line values.
- BlueJ accepts void main(); full form public static void main(String args[]) works everywhere.
- ; ends statements; braces in pairs; Java is case sensitive.
Basic Structure of a Class in Java: frequently asked questions
Should I write void main() or public static void main(String args[]) in my exam?
CISCE treats public, static and String args[] as default, so void main() is acceptable. The full form is always correct and works on every Java version outside BlueJ, so many teachers prefer it. Follow your teacher's instruction and be consistent.
Must the file name match the class name?
Yes for a public class, and it is good practice always. BlueJ creates the file for you with the class name.
Can a program have more than one class?
Yes, but at this stage one class with main() is enough. Later you will write classes with several methods and objects.
Based on: CISCE ICSE 2028 Computer Applications (86) syllabus (Classes IX and X); CISCE Regulations for ICSE Examination Year 2028