Basic structure of a class in java
ICSE Class 9 · Computer Applications · Chapter 5
What this chapter covers
- Why every Java program has a fixed structure
- The class header and the class body
- The main() method
- The set of statements inside main()
- Comments: the non-executable part of a program
- The default keywords: public, static and String args[]
- The import statement: import java.util.*;
- The Scanner object and accepting values
- Designing any program with the standard structure
- Common mistakes in the structure of a program
Key points
- A Java program is written inside a class, and its basic structure is: comments, the import statement when needed, the class header, and the class body containing `main()`.
- The class header is the keyword `class` followed by a valid identifier; by convention a class name begins with a capital letter.
- A public class must be saved in a file with exactly the same name and the extension `.java`.
- The `main()` method is the starting point of execution; statements in it run one after another from top to bottom.
- Every simple statement ends with a semicolon, and every opening brace needs a matching closing brace.
- Comments are non-executable notes for human readers: `//` for a single line and `/* … */` for several lines.
- The ICSE short header `void main()` leaves out the default keywords `public`, `static` and `String args[]`; the full header is `public static void main(String args[])`.
- `public` lets the JVM reach `main()`, `static` lets it be called without an object, `void` means no value is returned, and `String args[]` receives words given when the program starts.
- `import java.util.*;` is written above the class header to make the `Scanner` class available.
- Inside `main()`, the input form of the structure follows four steps: declare variables, create `Scanner sc = new Scanner(System.in);`, accept values with methods such as `nextInt()`, `nextDouble()` and `nextLine()`, then process and display.
- A prompt shown with `print()` before each input tells the user what to type.
- A dry run with sample values checks a program by hand before it is run.
Questions answered
What is meant by the basic structure of a Java program? Name its parts in order.
The basic structure is the standard arrangement of the parts of a program. In order, they are: comments describing the program, the import statement (when `Scanner` is used), the class header, and the class body containing the `main()` method with its statements.
Define a class header and a class body.
A class header is the first line of a class definition, made up of the keyword `class` and the class name, as in `class Bill`. A class body is everything enclosed between the opening and closing curly braces that follow the header.
Why is the `main()` method needed in a program that is to be run?
Execution of a Java program begins at the first statement of `main()` and ends at its closing brace. Without `main()`, the class can be compiled but there is no starting point from which to run it as a program.
Under what condition must a Java file have the same name as its class? What would the file be called for `public class Marks`?
A file must have exactly the same name as its class when the class is declared `public`. The file for `public class Marks` must be saved as `Marks.java`.
What is a comment? Why are comments called non-executable?
A comment is a note written in a program for human readers. It is called non-executable because the compiler ignores it, so it is not converted into byte code and has no effect when the program runs.
Give one example each of a single-line comment and a multi-line comment.
`// finds the area of a circle` is a single-line comment. `/* Program to find the area of a circle, written for Class IX */` is a multi-line comment, and it may be spread over several lines.
The syllabus says that `public`, `static` and `String args[]` are default in nature. Explain the purpose of each.
`public` is an access specifier that lets the Java Virtual Machine, which is outside the class, call `main()`. `static` lets `main()` be called through the class without first creating an object. `String args[]` is the parameter that receives words typed when the program is started. In the ICSE short form `void main()`, these may be left out.
Why can `void` not be left out of the header of `main()`?
`void` is the return type of the method, showing that `main()` returns no value. Every method header in Java must state a return type, so `void` is written in both the short and the full form of the header.