Skip to content
Computer ScienceISC Class 12 · Programming in Java (Review of Class XI Sections B and C)

How Java Runs and the Structure of a Java Class (ISC Class 12 Computer Science)

Updated 18 October 2026 · Draft, not yet fact-checked

A Java source file is compiled by javac into bytecode, which the JVM on any computer interprets or compiles to machine code, making Java platform independent. A class has data members, constructors, methods and main( ), which is public, static and void and takes String[] args. Identifiers must not start with a digit or be keywords, and Java is case-sensitive.

Notice that javac makes one bytecode file, and each computer's own JVM runs it, so Java is platform independent.

Illustration drawn for QuestionBuddy.

Understand How Java Runs and the Structure of a Java Class

1. Source, bytecode and the JVM

1. Source code is written in a .java file.
2. The compiler (javac) translates it into bytecode in a .class file, an intermediate code not specific to any processor.
3. The Java Virtual Machine on each computer interprets (and just-in-time compiles) the bytecode into machine instructions.

So the same .class file runs on any system with a JVM: platform independence, 'write once, run anywhere'.

2. Other features. Object-oriented; robust (strong type checking, exception handling, automatic garbage collection); secure; supports multithreading; a large standard library (java.lang, java.util, java.io).

3. JDK, JRE, JVM. The JDK (Java Development Kit) has the compiler and tools. The JRE (Java Runtime Environment) has the JVM and libraries needed to run programs. The JVM executes bytecode. BlueJ is an IDE that lets you create objects interactively without writing main( ), but board answers must still include main( ).

4. Structure of a class

import java.util.Scanner;

public class Name
{
    int a;                 // data member (instance variable)
    static int count;      // class variable

    Name()                 // constructor
    {
        a = 0;
    }

    int twice(int x)       // method
    {
        return x * 2;
    }

    public static void main(String[] args)
    {
        Name ob = new Name();
        System.out.println(ob.twice(6));
    }
}

This prints 12 (twice(6) = 6 x 2). The program was not compiled here; the output is traced by hand.

5. Key points

  • Only one public class per file, and the file name must match it.
  • main( ) is public, static, void and takes String[] args; execution starts there. It is static so that the JVM can call it without creating an object.
  • Statements end with semicolons; blocks are enclosed in braces.
  • Comments: // single line, /* multi-line */, /** documentation */.
  • Identifiers: letters, digits, underscore and the currency sign; cannot start with a digit; cannot be a keyword; Java is case-sensitive, so total and Total are different.
  • Naming conventions: classes in PascalCase (KaprekarRange), methods and variables in camelCase (isKaprekar), constants in UPPER_CASE.

6. Valid or invalid identifiers. total_1 valid; 1total invalid (starts with a digit); class invalid (keyword); my-var invalid (hyphen); Total and total are two different names.

7. Why bytecode helps. The developer compiles once and ships one .class file. To run it on 3 platforms, each platform needs only its own JVM; without bytecode the program would have to be compiled separately 3 times, once for each platform.

How Java Runs and the Structure of a Java Class: key facts

Execution path
  • Source (.java)
  • javac
  • Bytecode (.class)
  • JVM
  • Machine code
Platform independence from bytecode.
main method header
  • public
  • static
  • void
  • main
  • String array args
Every word has a reason.
Identifier rules
  • Start with letter or underscore
  • No keywords
  • Case-sensitive
  • No spaces or hyphens
Digits allowed after the first character.

How to answer How Java Runs and the Structure of a Java Class questions

Questions ask for the role of the compiler and JVM, why main is static, identifier validity, or what a given small class prints.

  1. 1For execution questions, describe the order: source, compiler, bytecode, JVM.
  2. 2For identifiers, test first character, keywords and special characters.
  3. 3For a small class, start at main( ) and follow the constructor and calls in order.
  4. 4State the output exactly with its type, for example 12 or 12.0.

How Java Runs and the Structure of a Java Class 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.

Platform independence

Suppose a school writes a library program on a Windows laptop and wants to run it unchanged on its Linux server and a classroom Mac.

Why does Java allow this?

Try it first, then show the solutionSolution
  1. The compiler produces bytecode that is not tied to a processor.
  2. Each machine has its own JVM to run that bytecode.

Answer: Bytecode plus a JVM on each machine gives platform independence.

Memory hook: Compile once, run anywhere.

Static main

Imagine a cinema opens its doors before any ticket is issued: the staff start the show without waiting for a customer.

How does this resemble the static main( ) method?

Try it first, then show the solutionSolution
  1. The JVM must start the program before any object exists.
  2. A static method belongs to the class and can be called without an object.

Answer: main( ) is static so it can run before any object is created.

Memory hook: The program starts before objects exist.

Tricks to remember How Java Runs and the Structure of a Java Class

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

  • S-B-J

    Source, Bytecode, JVM.

    Use it when: Order of execution.

  • JDK builds, JRE runs

    The JDK contains tools to build; the JRE contains the JVM to run.

    Use it when: JDK versus JRE.

How Java Runs and the Structure of a Java Class: solved examples

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

Example 1: Valid identifier · 1 mark · MCQBoard level

Which of the following is a valid Java identifier?
(a) 2marks
(b) total-marks
(c) class
(d) total_marks

Try it first, then show the solutionSolution
  1. (a) starts with a digit; (b) has a hyphen; (c) is a keyword; (d) is valid.

Answer: (d)

Example 2: Why bytecode · 2 marks · Short answerBoard level

A programmer compiles Hello.java on a Windows PC and emails Hello.class to a friend with a Linux laptop that has Java installed. Explain what file is sent, and why it runs there without recompiling.

Try it first, then show the solutionSolution
  1. The .class file holds bytecode, the machine-independent intermediate code produced by javac.
  2. The Linux laptop's JVM executes that bytecode, so no recompilation is needed.

Answer: Bytecode, run by the JVM of the Linux machine.

Example 3: Trace a small class · 4 marks · Short answerHarder board level

In the class Name above, main( ) creates ob and prints ob.twice(6). Give the output. Then predict the output if main( ) prints ob.a + ob.twice(ob.a + 4).

Try it first, then show the solutionSolution
  1. twice(6) = 6 x 2 = 12, so the first output is 12.
  2. The constructor sets a = 0.
  3. ob.twice(ob.a + 4) = twice(4) = 8.
  4. ob.a + 8 = 0 + 8 = 8, so the second output is 8.

Answer: 12 and 8.

Example 4: Identifiers and conventions · 4 marks · Short answerHarder board level

Classify as valid or invalid identifiers, with reasons: (i) _count, (ii) 3rdYear, (iii) isPrime, (iv) new, (v) Total and total. Say whether (v) refers to one variable or two.

Try it first, then show the solutionSolution
  1. (i) Valid: begins with an underscore.
  2. (ii) Invalid: begins with a digit.
  3. (iii) Valid, and follows camelCase.
  4. (iv) Invalid: new is a keyword.
  5. (v) Both valid; Java is case-sensitive, so they are two different variables.

Answer: (i) valid, (ii) invalid, (iii) valid, (iv) invalid, (v) two variables.

Example 5: Explain the structure · 8 marks · Long answerHOTS / competency

Suppose a student writes a program in a file Test.java whose public class is called Demo, with main( ) declared as 'public void main(String[] args)'. List all the errors and explain how to fix them, and explain the role of each word in the correct main header.

Try it first, then show the solutionSolution
  1. Error 1: only one public class per file and the file name must match it, so rename the file Demo.java (or the class Test).
  2. Error 2: main( ) is missing static, so the JVM cannot start it without an object; correct header: public static void main(String[] args).
  3. public: the JVM must be able to call it from outside the class.
  4. static: it can be called without creating an object.
  5. void: it returns no value to the JVM.
  6. main: the name where execution starts.
  7. String[] args: command-line arguments passed as an array of strings.
  8. After fixing, compile with javac, producing Demo.class, and run on the JVM.

Answer: File name must match the public class; main must be static; each word explained.

Where students lose marks in How Java Runs and the Structure of a Java Class

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

  • Mistake: Saying the JVM compiles source code

    javac compiles; the JVM executes bytecode.

    Wrong working

    The JVM converts the .java file into bytecode.

    Right working

    The compiler javac converts .java to bytecode; the JVM runs the bytecode.

    Do this: javac compiles, JVM runs.

    Usually costs: 1 mark.

  • Mistake: Writing main without static

    The JVM needs it without an object.

    Wrong working

    public void main(String[] args).

    Right working

    public static void main(String[] args).

    Do this: Memorise the header.

    Usually costs: 1 mark.

  • Mistake: Treating Total and total as the same

    Java is case-sensitive.

    Wrong working

    Total and total are the same variable.

    Right working

    They are two different identifiers.

    Do this: Check capitals.

    Usually costs: 1 mark.

Exam tips

  • Describe the path source, javac, bytecode, JVM in order.
  • Write the main header exactly.
  • For output questions, start from main( ) and apply the constructor first.
  • BlueJ can run without main( ) but written answers must include it.

Practice questions on How Java Runs and the Structure of a Java Class

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 · Very short answerBoard level

    What does JVM stand for?

    Hint

    Virtual.

    Answer

    Java Virtual Machine.

  2. Q2 · 2 marks · Short answerBoard level

    State two differences between the JDK and the JRE.

    Hint

    Build versus run.

    Answer

    The JDK contains the compiler and development tools, whereas the JRE contains the JVM and libraries needed only to run programs.

  3. Q3 · 2 marks · Short answerHarder board level

    Why is main( ) declared public?

    Hint

    Who calls it?

    Answer

    The JVM calls main( ) from outside the class, so it must be publicly accessible.

  4. Q4 · 4 marks · Short answerHarder board level

    Predict the output: class P { int v; P() { v = 5; } int f(int k) { return v * k + 1; } public static void main(String[] a) { P q = new P(); System.out.println(q.f(3)); System.out.println(q.f(q.v)); } }

    Hint

    v is 5 after the constructor.

    Answer

    f(3) = 5 x 3 + 1 = 16. f(q.v) = f(5) = 5 x 5 + 1 = 26. Output lines: 16 and 26.

  5. Q5 · 4 marks · Short answerHOTS / competency

    A student says, 'Because Java is compiled, a .class file will not run on a different operating system.' Evaluate the statement.

    Hint

    What does the compiler produce?

    Answer

    The statement is wrong for Java. The compiler produces bytecode, not machine code for one processor. Each operating system has its own JVM that executes the bytecode, so the same .class file runs on any system with a JVM, which is platform independence.

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.What does javac produce?Show answer

    Bytecode in a .class file.

  2. 2.Which contains the JVM: JDK or JRE?Show answer

    The JRE (the JDK includes it as well).

  3. 3.May an identifier start with a digit?Show answer

    No.

  4. 4.How many public classes per file?Show answer

    One, with a matching file name.

How Java Runs and the Structure of a Java Class: frequently asked questions

Is Java interpreted or compiled?

Both: compiled to bytecode and then interpreted or just-in-time compiled by the JVM.

Which Java version does the syllabus mention?

Java 5.0 or later, per the chapter page.

Where do number programs appear?

On the operators, control flow and number programs topic page.