Basic structure of a class in java | ICSE Class 9 Computer Applications Notes
On this page
This note covers Java class structure, comments, class declarations, method headings, braces, parameters, imports, Scanner input, the order of statements, and checks for reading and completing a class outline.
What is the basic structure of a class in Java?
Java is a programming language. A program is a set of instructions written to perform a task. A class is a specification for objects, where an object is an instance created from a class.
A data type specifies a kind of value used in a program. A class may be regarded as a blueprint to create objects. It may be viewed as a factory that produces similar objects. It may also be considered a user-defined data type, meaning a type created by the programmer with its own functionality.
Data means the values a program works with. Objects have state, represented by their attributes or stored characteristics, and behaviour, represented by methods. A method is a named part of a class that defines an operation.
What belongs inside the outline?
The basic outline has a class heading, an opening brace, a method heading, the method's opening brace, its statements, and the corresponding closing braces. A statement is an instruction or declaration written according to the language's syntax, meaning its rules for valid program text.
A heading introduces the class or method. A body is the region enclosed by its braces. A brace is a curly bracket: { opens the body and } closes it. The method body lies within the class body.
Definition: Class structure is the arrangement of the class heading, its body, and the declarations and methods written within that body.
A declaration introduces a named program element and information about it. A variable is a named storage location whose value can change. Declarations and executable statements play different roles even when they appear together inside a method.
Execution means carrying out program instructions. Reading the structure tells us which statements belong to a method; it does not, by itself, tell us what result those statements will produce. The operations and the values supplied are also needed.
Learn the outline from the outside inward. First identify the class boundary, then the method boundary, and finally the statements inside the method. This keeps the arrangement clear when the number of statements changes.
How should the parameter-based class outline be read?
A parameter is a variable declared in a method heading to receive a value when the method is called. A method call requests that the method perform its operation. The value supplied in that call is an argument.
The word void indicates that the method does not return a value to its caller. Here main is the method's name. The parentheses, ( and ), enclose the parameter list in the method heading.
In the outline below, <name of the class> and <parameters> are placeholders: descriptions of information to replace. Their angle brackets mark those placeholders; they are not characters to retain around a class name or parameter declaration in the completed program.
The symbol // begins a single-line comment, which is explanatory text rather than an executable instruction. The words “Set of statements” describe where the instructions belong. They are not a Java instruction to copy literally.
Syntax: Parameter-based class outline.
//comment
class <name of the class>
{
void main(<parameters>)
{
Set of statements
}
}
What does each part do?
- The comment provides information for a person reading the program. It does not perform the method's task.
- The word class introduces the class declaration, followed by the chosen class name.
- The first opening brace begins the class body, which contains the method shown here.
- The method heading gives the return type, method name and parameter list. A return type describes the kind of value returned, with void indicating no returned value.
- The inner opening brace begins the method body, where its statements are written.
- The inner closing brace ends the method; the outer closing brace ends the class.
This is a structural outline, not a complete executable program. Completing it requires actual declarations and statements. A result cannot be calculated from the phrase “Set of statements”, because that phrase specifies neither an operation nor the data used.
What do comments contribute to a class?
A comment explains a program to its reader without becoming an instruction that the program executes. A comment can identify the purpose of a section or explain what a nearby statement is intended to do.
In the two basic class outlines, a comment appears before the class declaration. The input outline uses Scanner, a Java library class for reading and interpreting input. A library class is a class supplied for reuse. Input means data supplied to a program.
That outline also places a comment after its import declaration, which allows specified types to be referred to without their package prefix. A package is a named grouping of related Java types. The prefix identifies the package to which a type belongs.
How are comment boundaries recognised?
Outside quoted text, // starts a comment that continues to the end of that source line. Source means the program text written by the programmer. A line break ends this form of comment; a closing brace does not.
A multiline comment starts with /* and ends with */. These paired markers enclose explanatory text that may extend across lines. The start and end markers have different orders, so their positions matter.
| Form | Beginning | End | Role |
|---|---|---|---|
| Single-line comment | // | End of source line | Explains text on that line without executing it |
| Multiline comment | /* | */ | Encloses explanatory text that may occupy several lines |
Rule: Text inside a comment does not supply an executable statement. A description of an operation and an instruction performing that operation are different things.
The Scanner outline includes the comment “when Scanner class is used”. It explains the reason for the preceding import. It does not create a Scanner object, accept data, or change the order in which the method's statements execute.
When checking braces, first establish which characters belong to comments. A brace written inside a comment is explanatory text, not a class or method boundary. Likewise, a statement written entirely inside a comment will not carry out its intended operation.
How do the class heading and punctuation organise the body?
A keyword is a word reserved by Java for a language-defined purpose. In the class heading, class is a keyword; the name following it is an identifier, meaning a name used to identify a program element.
Keep these roles separate. The keyword states what kind of declaration follows. The identifier distinguishes this class from other classes. The descriptive phrase inside the class-name placeholder must be replaced when completing the outline.
How are nested boundaries read?
Nesting means placing one structure inside another. In these outlines, the method body is nested inside the class body. Its opening and closing braces form an inner pair, while the class braces form the surrounding pair.
Match each closing brace with the most recent opening brace that has not yet been closed. The method closes before the class. Simply seeing the same number of opening and closing braces is not enough if those braces enclose the wrong text.
Indentation means the spaces used at the start of a line to show its level within a structure. Indenting method statements makes their enclosure visible to a reader. The braces, rather than the indentation alone, establish the boundaries.
| Symbol | Name | Role in the outline |
|---|---|---|
| { and } | Opening and closing braces | Enclose class and method bodies |
| ( and ) | Parentheses | Enclose the method's parameter list |
| ; | Semicolon | Terminates the import declaration shown in the Scanner outline |
The semicolon, written ;, also terminates variable declarations and many simple statements. It is not a substitute for a brace. The basic class and method headings shown here are followed by bodies, not by semicolons.
Rule: Read punctuation by its function: parentheses surround the parameter list, braces enclose bodies, and a semicolon terminates declarations or statements that require it.
When copying an outline, preserve its enclosure relationships even if spacing changes. A method's statements must remain inside that method's body, and the method declaration must remain inside the class body shown.
What do main, void and the method parameters mean?
The method heading brings together the method name, its return information and its parameter list. In void main(<parameters>), void gives the return information, main supplies the name, and the parenthesised placeholder indicates where parameter declarations belong.
Returning a value means supplying a result back to the code that called the method. Displaying text is a different operation. A void method may display information while returning no value to its caller.
How do parameters supply values?
A parameter declaration gives a data type and a name. A data type specifies the kind of value a variable can hold. The parameter receives a corresponding argument value when the method is called, making that value available within the method.
In void main(), the empty parentheses indicate that this method declares no parameters. They do not mean that the method has no statements. Its instructions belong between the braces that follow the heading.
Similarly, empty parentheses do not prove that a program cannot accept input. Input is data supplied to a program. A method without parameters can still contain statements that accept input during its execution.
How does the familiar application heading differ?
A commonly taught Java application entry heading is public static void main(String args[]). An entry method is the method selected to begin running an application. This fuller heading must not be confused with every possible method named main.
public permits access from outside the class. static associates the method with the class, allowing it to be called without first creating an instance of that class. Both words are modifiers, meaning words that qualify a declaration.
String is Java's type for character strings, or sequences of characters. Here args names the parameter. The square brackets [] indicate an array, a structure holding a sequence of elements of the same component type.
In that fuller heading, args receives the command-line arguments, which are text values supplied when launching the application. String args[] is a parameter declaration; it is not a single Java keyword.
Note: A shortened teaching outline does not mean that Java automatically inserts omitted public or static modifiers. Treat the outline as a structural guide and distinguish it from the entry declaration required by the way the program is launched.
Why does the Scanner outline begin with an import?
Scanner belongs to java.util, the package used in the input outline. Naming that package identifies where the reusable Scanner type is organised. The program must then refer to that type in a way Java can resolve.
An import declaration allows certain types from another package to be referred to by their simple names. A simple name is the name without its package prefix. The Scanner outline places the import before the class declaration.
Syntax: Import used in the Scanner outline.
import java.util.*;//when Scanner class is used
Here import is the keyword introducing the declaration. The dots separate the parts of the package name and the final import pattern. In this context, * is a wildcard, which allows accessible types in java.util to be referred to by simple name as needed.
The semicolon ends the import declaration. The following // begins the explanatory comment. The import and comment therefore have different roles even though they occupy the same displayed line.
What does importing leave to the method?
Importing does not create an object and does not read a value. Object creation produces an instance of a class. Reading input obtains data from an input source. These are separate steps in the Scanner-based outline.
| Part | Position in this outline | Purpose |
|---|---|---|
| Import declaration | Before the class heading | Makes types such as Scanner available by simple name |
| Scanner object creation | Inside the method body | Creates the object through which input can be read |
| Scanner method calls | Inside the method body after creation | Accept the required values from input |
The wildcard in the import is not a multiplication operation. Its meaning comes from its position in the import declaration. Likewise, the import does not mean that every class in the package becomes an object in the program.
The basic distinction is between making a type's name available and using an object of that type. Remembering this distinction explains why the outline needs both its import line and its later object-creation step.
How is the Scanner-based class outline arranged?
The Scanner-based outline uses the same outer class boundary and inner method boundary as the parameter-based outline. Its method heading has empty parentheses. The steps for obtaining values appear within the method body instead of within a parameter list.
The descriptive lines below are placeholders for actual Java declarations and statements. They specify the roles and order of the parts. They do not provide executable input instructions or a complete program whose output can be calculated.
Syntax: Scanner-based class outline.
//comment
import java.util.*;//when Scanner class is used
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
}
}
Why does the sequence matter?
- Declare the variables. Establish the names and data types used to hold the required values. A declaration identifies the storage needed; it does not necessarily supply a value.
- Create the Scanner object. Establish the object through which the input methods will be used. The import line alone has not performed this step.
- Accept the required values. Use the appropriate Scanner methods to obtain input and place the values where the later statements can use them.
- Perform the required processing. Carry out the statements for the task using the values that have been supplied. Processing means performing operations on data.
An input method is a method used to obtain data. Scanner provides different methods for different input requirements. The structural point here is the placement of these calls after object creation and before the later statements that depend on their results.
The outline separates declaration, creation, input and processing to make the sequence clear. Do not replace the entire sequence with the word Scanner: that is a class name, not an instruction describing all the steps.
After the statements, close the method body and then the class body. The import remains outside both bodies. The same enclosure pattern therefore supports a different way of supplying the data.
How do parameters and Scanner input differ?
Parameter input supplies values through a method call. Scanner input obtains values through input-reading operations performed while the method executes. Both can provide data used by statements, but their positions in the class outline differ.
In the parameter-based outline, the method heading identifies the receiving parameters. In the Scanner-based outline, the method shown has no parameters and contains the object-creation and input-reading steps inside its body.
What changes and what stays the same?
| Feature | Parameter-based outline | Scanner-based outline |
|---|---|---|
| Receiving values | Through parameters when the method is called | Through Scanner methods during method execution |
| Method heading shown | Contains a parameter placeholder | Contains empty parentheses |
| Additional input setup shown | No Scanner setup in the displayed outline | Import and Scanner object creation are included |
| Class boundary | Outer pair of braces | Outer pair of braces |
| Method boundary | Inner pair of braces | Inner pair of braces |
Initialisation means giving a variable its initial value. It is distinct from merely declaring the variable. Parameter passing, initialisation and Scanner input must not be treated as interchangeable labels for every appearance of data in a program.
For a parameter-based method, ask what values the call supplies and which declarations receive them. For the Scanner-based method, ask where the input source is used and which statements read the required values.
Note: The absence of parameters in a method heading does not establish the absence of input. Read the method body before deciding how values enter the computation.
The two outlines illustrate different arrangements; they do not establish a rule that parameters and Scanner can never occur together. The structure used for a particular task depends on how that task is designed to obtain its values.
When explaining a supplied outline, describe the arrangement actually shown. Do not assume unseen parameter values, input data, or additional statements. An explanation of the structure can be complete even when a numerical result cannot be determined.
How can a class outline be checked before execution?
Syntax means the rules governing the form of valid program text. A syntax error violates those rules. Compilation is the translation and checking stage used to prepare Java source for execution; a compiler performs that stage.
Start a structural check with the declaration and its boundaries. Then check the method heading, its body, and the placement of the statements. Finally, check how the required values are supplied and whether the instructions use them in the intended order.
Which checks should be made in sequence?
- Identify the class keyword and the actual class name. Ensure that the descriptive class-name placeholder has been replaced.
- Locate the class's opening brace and its matching closing brace. Confirm that the method lies inside them.
- Read the method heading. Distinguish its return information, name and parameter list from the statements inside its body.
- Match the method braces and inspect required semicolons. Do not count comment text as executable structure.
- For the Scanner arrangement, check the import, variable declarations, object creation and input-reading steps in their proper positions.
- Replace descriptive placeholders with actual Java statements before attempting compilation or execution.
A runtime error occurs while a program is executing. A logical error makes the program's behaviour differ from the intended solution. Correct brace matching helps with structure but does not prove that the program's calculations or other actions are correct.
Can the outline determine an output?
Output is information produced by a program. Neither outline fixes an output, because neither supplies the actual set of statements and values needed to determine one. Do not turn a structural placeholder into an assumed calculation.
To explain output from a complete program, follow the actual statements and use every input value supplied. Distinguish a method returning a value from a method displaying information. The word void alone cannot establish that nothing will be displayed.
Rule: Check structure and behaviour separately. A well-arranged class still needs suitable statements and data to perform the intended task.
The standard class structure could be followed when designing other Java programs. Its reusable feature is the arrangement of declarations and bodies. The actual processing statements must express the task being performed.
Glossary
- Class — A specification from which objects with associated state and behaviour can be created.
- Object — An instance of a class that has state and behaviour.
- Method — A named part of a class defining an operation performed when called.
- Declaration — Program text introducing a named element and information about that element.
- Variable — A named storage location whose value can change during program execution.
- Parameter — A variable declared in a method heading to receive a supplied argument value.
- Argument — A value supplied in a method call for a corresponding parameter.
- Comment — Explanatory source text that does not become an executable program instruction.
- Identifier — A name used to identify a program element, such as a class.
- Keyword — A word reserved by Java to serve a language-defined purpose.
- Package — A named grouping of related Java types, including reusable library classes.
- Scanner — A Java library class used for reading and interpreting input data.
- Initialisation — The act of giving a variable its initial value in a program.
- Syntax — Rules that govern the form and arrangement of valid program text.
- Execution — The process of carrying out the instructions supplied by a program.
Common errors and misconceptions
- Misconception: The phrase “Set of statements” is a Java instruction. Correct: It is a structural placeholder that must be replaced by actual statements for the task.
- Misconception: The class and its method share a single body boundary. Correct: Each has its own pair of braces, with the method body enclosed within the class body.
- Misconception: A comment accepts or processes data when the method runs. Correct: A comment explains the program; its text does not execute as an instruction.
- Misconception: Importing java.util creates a Scanner object and reads input. Correct: The import, object creation and input-reading calls have separate roles.
- Misconception: Empty method parentheses mean that no input is possible. Correct: They indicate no declared parameters; the method body may still read input through Scanner.
- Misconception: void means that a method cannot display anything. Correct: It means no value is returned to the caller. Displaying output is a different operation.
- Misconception: Java automatically supplies public and static whenever an outline omits them. Correct: A shortened teaching outline does not establish such a language rule.
- Misconception: Balanced braces prove that the result is correct. Correct: The statements, their order and the values used must also be checked against the intended task.
Exam-style questions with model answers
Q1. State the role of a class and the role of a method within it. [2 marks]
- A class is a specification from which objects may be created.
- A method defines an operation within the class and contains the statements for that operation.
Q2. In the heading void main(), explain void and the empty parentheses. [2 marks]
- void indicates that the method returns no value to its caller; it does not prohibit displayed output.
- The empty parentheses indicate that this method declares no parameters to receive argument values.
Q3. Explain three distinct parts of the line import java.util.*;//when Scanner class is used: the import keyword, java.util.*, and the comment beginning with //. [3 marks]
- The import keyword introduces an import declaration, allowing specified types to be referred to by their simple names.
- java.util.* allows accessible types in the java.util package, including Scanner, to be referred to by simple name as needed.
- The text after // is a single-line comment explaining the use of the import; it does not create an object or read data.
Q4. A Scanner-based method is to declare variables, create a Scanner object, accept the required values, and then process those values. Explain the purpose of each of these four stages in that order. [4 marks]
- Declare the variables to establish the names and data types used for the values required by the method.
- Create a Scanner object so that the program has the object through which its input-reading methods can be used.
- Accept the required values using suitable Scanner methods, making those values available for the statements that need them.
- Process the supplied values by executing the statements that perform the intended task, after the required input has been obtained.
Q5. Describe five structural parts of an outline consisting of a comment, class heading, class body, void main(<parameters>) heading, and method body. Treat the angle-bracketed text as a placeholder, and explain where statements and closing braces belong. [5 marks]
- The comment supplies explanatory information for the reader. Its text is non-executable and does not carry out the task described by the method.
- The class heading contains the class keyword followed by the class name. It introduces the class whose body follows within the outer braces.
- The class body starts with an opening brace and contains the method declaration. Its closing brace comes after the method has been closed.
- The method heading uses void to indicate no returned value, main as the method name, and a parenthesised placeholder for actual parameter declarations.
- The method body contains the actual statements between its own braces. Its closing brace ends those statements before the final brace closes the class.
Q6. Compare these two outlines in three respects: outline A has void main(<parameters>) and a set of statements; outline B imports java.util.*, has void main(), creates a Scanner object, accepts values through its methods, and then executes a set of statements. Both place the method inside class braces. [3 marks]
- Outline A receives values through parameters supplied when the method is called. Outline B reads values through Scanner methods during execution.
- Outline A shows parameters in its method heading. Outline B shows empty parentheses and places the input-reading operations within the method body.
- Both outlines enclose the method within the class. Outline B additionally shows the import and Scanner object creation required by its displayed arrangement.
Q7. A class outline supplies only a class-name placeholder, void main(<parameters>), braces, and the words “Set of statements”. It supplies no actual parameter values or statements. Can its displayed output be determined? Give two reasons for your conclusion. [2 marks]
- No: the actual statements are missing, so no operation or display instruction has been specified.
- The required argument values are also missing, so there are no supplied data from which a result could be determined.
Q8. Explain six parts of the familiar Java application heading public static void main(String args[]): public, static, void, main, String, and args[]. [6 marks]
- public is an access modifier permitting the method to be accessed from outside its class. It qualifies the declaration rather than naming the method.
- static associates the method with the class, so calling it does not require first creating an instance of that class.
- void indicates that this method does not return a value to its caller. It does not prevent the method from displaying output.
- main is the method name in this entry declaration, identifying the method selected when the application is launched in the familiar way.
- String is the Java type representing character strings. It supplies the component type of the array declared as the method parameter.
- args is the parameter name and [] indicates an array. Together they declare the receiving parameter for the command-line text arguments.
Key takeaways
- A class may be regarded as a blueprint for objects, with methods defining operations associated with the class.
- The class body surrounds the method declaration, while the method body surrounds its own set of statements.
- Comments explain source text without executing; comment markers must be distinguished from the punctuation of program instructions.
- The method heading identifies the method, states its return information and encloses its parameter declarations within parentheses.
- void means that no value is returned to the caller; it does not establish whether information is displayed.
- The Scanner outline separates importing, declaring variables, creating an object, accepting input and processing the required values.
- Parameters receive values through method calls, whereas Scanner methods obtain values through input operations during execution.
- A structural outline cannot determine output without actual statements and the values needed by those statements.
Test yourself
Which closes first in the basic outline: the method body or the class body?
The method body closes first because it is enclosed within the surrounding class body.
What does the class keyword introduce?
It introduces a class declaration, followed by the class name and the body of that class.
What ends a comment beginning with // outside quoted text?
The end of the source line ends the comment; a brace within that comment does not close a program body.
Does import java.util.*; create an input-reading object?
No. It makes types available by simple name; creating a Scanner object is a separate step.
What does the parameter placeholder in void main(<parameters>) represent?
It represents actual parameter declarations, each specifying the type and name of a variable that receives an argument value.
Why may void main() still accept input during execution?
Empty parentheses mean no declared parameters. Statements inside the method can still read input through Scanner.
Is String args[] a single Java keyword?
No. It is a parameter declaration using the String type, the name args and square brackets indicating an array.
Why is matching braces insufficient to prove that a program solves its task?
Matching braces checks enclosure. The statements, input values and processing order must also produce the intended behaviour.
