Skip to content
Computer ApplicationsICSE Class 10 · Class as the Basis of All Computation

Instance and Static Variables, Primitive and Composite Types (ICSE Class 10 Java)

Updated 17 October 2026 · Fact-checked

An instance variable belongs to each object: every object has its own copy. A class (static) variable is declared with static and has one copy shared by all objects of the class; it can be used with the class name. A primitive type holds its value directly; a composite type (a class) holds a reference to an object, so copying it copies the reference, not the object.

Instance variables are separate in each object, a static variable is shared by all, and copying a reference leaves x and y pointing to one object.

Illustration drawn for QuestionBuddy.

Understand Instance and Static Variables, Primitive and Composite Types

Members of a class. Variables inside a class but outside any method are member variables (attributes); they define the state. Methods are member methods; they define behaviour. A class is an abstraction for a set of similar objects and acts as a factory that makes them.

Instance variables. Declared without static. Each object created with new gets its own copy. Changing a.id does not change b.id.

Class (static) variables. Declared with static. Only one copy exists for the whole class, shared by all objects, and it exists even before any object is made. Use it for things common to the class, such as a counter of objects: Item.total. Access with the class name is the usual style.

Default values. Member variables get defaults when not initialised: numbers 0 (or 0.0), boolean false, char the null character, references null.

Primitive vs composite data types.

  • Primitive: int, double, char, boolean and the others: hold one simple value. int b = a; copies the value, and changing b later does not affect a.
  • Composite (class types): String, arrays and your own classes. A variable holds a reference to an object. Box y = x; copies the reference, so x and y name the same object, and a change through y is seen through x.

Declaring both. int n = 5; for primitive, Box x = new Box(); for composite (the variable is of the user-defined type Box).

Class design routine. (1) list the attributes as member variables, (2) write methods that set and use them, (3) create objects in main and call the methods with the dot operator.

Instance and Static Variables, Primitive and Composite Types: key facts

Instance vs static
Instance: one copy per object, no static keyword | Static: one copy for the class, declared with static, shared by all objects
Access static as ClassName.variable.
Default values
  • int 0
  • double 0.0
  • boolean false
  • reference null
Applies to member variables, not local variables.
Primitive vs composite
Primitiveholds the value
Compositeholds a reference to an object
Copy of primitiveindependent
Copy of referencesame object
String and arrays are composite.
Class members
  • Member variables = state
  • Member methods = behaviour
  • Object = instance of the class
Use real-life examples to explain.

How to answer Instance and Static Variables, Primitive and Composite Types questions

Use this routine for class and object output questions.

  1. 1List the objects created and write their instance variables (with default values) in a table.
  2. 2Keep one separate cell for each static variable, shared by all objects.
  3. 3Execute each call on the object named before the dot, updating only that object's cells (and the static cell if the method changes it).
  4. 4For reference assignments, note that both names point to the same table.
  5. 5Evaluate the print statements with the final values.

Instance and Static Variables, Primitive and Composite Types 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.

Shared counter and personal numbers

Suppose a school issues admission slips: each slip has its own slip number, but the school office keeps one counter of the total slips issued.

Which one is an instance variable and which is static?

Try it first, then show the solutionSolution
  1. The slip number differs for every slip: instance variable.
  2. The total issued is one value for the whole school: static variable.

Answer: Slip number: instance. Total issued: static.

Memory hook: Personal = instance, common = static.

Tricks to remember Instance and Static Variables, Primitive and Composite Types

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

  • Static = Single copy

    static starts with S like single: one copy for all.

    Use it when: Instance vs static.

  • Photocopy vs arrow

    Copying a primitive photocopies the value; copying an object variable copies the arrow to the same object.

    Use it when: Primitive vs composite.

  • Class = cookie cutter

    The cutter (class) makes many cookies (objects); each cookie has its own decoration (instance variables).

    Use it when: Explaining class and object.

Instance and Static Variables, Primitive and Composite Types: solved examples

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

Example 1: Instance versus static in a Student class · 3 marks · Short answerBoard level

A class Student has the variables String name, int roll and static String school. State which are instance variables and which is a class variable, and how many copies exist if 40 student objects are created.

Try it first, then show the solutionSolution
  1. name and roll are instance variables: each object holds its own values. (1 mark)
  2. school is declared static, so it is a class variable shared by all objects. (1 mark)
  3. For 40 objects there are 40 copies of name, 40 copies of roll and only 1 copy of school. (1 mark)

Answer: name, roll: instance (40 copies each); school: class variable (1 copy).

Example 2: Changing a class variable · 3 marks · Short answerHarder board level

Write the output:

class Emp {
    static String company = "ABC";
    String name;
}
public class Test {
    public static void main(String[] args) {
        Emp e1 = new Emp();
        Emp e2 = new Emp();
        e1.name = "Ravi";
        e2.name = "Sita";
        Emp.company = "XYZ";
        System.out.println(e1.company + " " + e2.name + " " + e2.company);
    }
}
Try it first, then show the solutionSolution
  1. company is static: one copy shared by e1 and e2. name is an instance variable: e1 has Ravi, e2 has Sita. (1 mark)
  2. Emp.company = "XYZ" changes the single shared copy, so both objects now see XYZ. (1 mark)
  3. The output joins e1.company (XYZ), e2.name (Sita) and e2.company (XYZ). (1 mark)

Answer: XYZ Sita XYZ

Example 3: Primitive copy and reference copy · 3 marks · Short answerHarder board level

Write the output:

class Box {
    int v;
}
public class Test {
    public static void main(String[] args) {
        int a = 5;
        int b = a;
        b = 10;
        Box x = new Box();
        x.v = 5;
        Box y = x;
        y.v = 10;
        System.out.println(a + " " + x.v);
    }
}
Try it first, then show the solutionSolution
  1. int b = a copies the value, so changing b leaves a equal to 5. (1 mark)
  2. Box y = x copies the reference; x and y name one object, so y.v = 10 changes x.v. (1 mark)
  3. The output is 5 for a and 10 for x.v. (1 mark)

Answer: 5 10

Example 4: Case study: a Rectangle class · 4 marks · Case-basedHOTS / competency

A class and a main method are written as:

class Rectangle {
    int l, b;
    void setValues(int x, int y) {
        l = x;
        b = y;
    }
    int area() {
        return l * b;
    }
    int perimeter() {
        return 2 * (l + b);
    }
}
public class Test {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        Rectangle r2 = new Rectangle();
        r1.setValues(4, 3);
        r2.setValues(10, 2);
        System.out.println(r1.area() + " " + r1.perimeter());
        System.out.println(r2.area() + " " + r2.perimeter());
    }
}

(i) Name the member variables and member methods. (1)
(ii) How many objects are created and what are their references? (1)
(iii) Write the output. (2)

Try it first, then show the solutionSolution
  1. (i) Member variables: l and b. Member methods: setValues(), area() and perimeter().
  2. (ii) Two objects with the references r1 and r2.
  3. (iii) r1: area 4 * 3 = 12, perimeter 2 * (4 + 3) = 14. r2: area 10 * 2 = 20, perimeter 2 * (10 + 2) = 24.

Answer: (i) l, b; setValues(), area(), perimeter() (ii) two objects, r1 and r2 (iii) 12 14 and 20 24

Example 5: Default values · 2 marks · Short answerHarder board level

Write the output:

class Data {
    int n;
    double d;
    boolean f;
    String s;
}
public class Test {
    public static void main(String[] args) {
        Data x = new Data();
        System.out.println(x.n + " " + x.d + " " + x.f + " " + x.s);
    }
}
Try it first, then show the solutionSolution
  1. Member variables get default values: 0, 0.0, false and null. (1 mark)
  2. Joining them with spaces prints null as the word null. (1 mark)

Answer: 0 0.0 false null

Where students lose marks in Instance and Static Variables, Primitive and Composite Types

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

  • Mistake: Treating a static variable as a separate copy per object

    There is only one copy; every object sees the same value.

    Wrong working

    a.total = 1 and b.total = 1 after each add()

    Right working

    total is 1 after a.add() and 2 after b.add() for both objects

    Do this: Keep one cell for each static variable.

    Usually costs: 1 to 2 marks

  • Mistake: Thinking a reference assignment makes a second object

    Only new creates an object.

    Wrong working

    Box y = x; then y.v = 10 leaves x.v = 5

    Right working

    x.v becomes 10 because x and y are one object

    Do this: Count the new keywords.

    Usually costs: 1 mark

  • Mistake: Forgetting default values in a trace

    Member variables start at 0, 0.0, false or null.

    Wrong working

    x.n is undefined

    Right working

    x.n is 0

    Do this: Write the defaults first.

    Usually costs: 1 mark

Exam tips

  • Give an example in the difference: a changing id per object and a shared counter.
  • In outputs, show a table with one column per object and one cell for each static variable.
  • Use real-life examples when asked to explain class and object (a bank account, a student).
  • String is a composite type even though it feels simple; say so in primitive vs composite answers.

Practice questions on Instance and Static Variables, Primitive and Composite Types

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 String variable in Java is an example of a:
    (a) primitive type (b) composite type (c) keyword (d) literal

    Hint

    String is a class.

    Answer

    (b) composite type

  2. Q2 · 1 mark · Very short answerBoard level

    Which keyword declares a class variable shared by all objects?

    Hint

    One copy for the class.

    Answer

    static

  3. Q3 · 2 marks · Short answerHarder board level

    Write the output:

    class Cnt {
        static int n;
        void hit() { n++; }
    }
    public class Test {
        public static void main(String[] args) {
            Cnt p = new Cnt();
            Cnt q = new Cnt();
            p.hit();
            q.hit();
            q.hit();
            System.out.println(Cnt.n + " " + p.n);
        }
    }
    Hint

    How many copies of n exist?

    Answer

    3 3 (one shared n, increased three times)

  4. Q4 · 3 marks · Short answerHarder board level

    Distinguish between primitive and composite data types with three points and an example of each.

    Hint

    Value or reference?

    Answer

    Primitive: holds a single simple value; copying copies the value; examples int, double, char. Composite: defined by a class; holds a reference to an object; copying copies the reference, so both names refer to one object; examples String, arrays and user-defined classes such as Box.

  5. Q5 · 1 mark · Assertion-reasonHOTS / competency

    Assertion (A): A static variable is shared by all objects of its class.
    Reason (R): Only one copy of a static variable exists for the whole class.
    (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.

    Hint

    Why is it shared?

    Answer

    (a) Both are true and R explains A.

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 is another name for a static variable?Show answer

    Class variable.

  2. 2.How many copies of an instance variable exist?Show answer

    One for every object.

  3. 3.What is the starting value of a boolean member variable that is never assigned?Show answer

    false

  4. 4.What does a composite variable hold?Show answer

    A reference to an object.

  5. 5.Which defines the state of an object: variables or methods?Show answer

    Variables.

  6. 6.How is a static variable accessed?Show answer

    With the class name, ClassName.variable.

Instance and Static Variables, Primitive and Composite Types: frequently asked questions

Can a static variable be changed through an object?

Yes, but it is the same single copy, so every object sees the change. The class name is the preferred way to use it.

Is an array primitive or composite?

Composite: an array variable refers to an array object.

What is a member method?

A method declared inside a class; it defines behaviour of the objects.