Static vs. instance members

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? public class Main { public static void main(String[] args) { new Robot(); new Robot(); System.out.println(Robot.count); } } class Robot { static int count = 0; Robot() { count++; } }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Static vs. instance members explained

    A static member belongs to the class itself and is shared by all of its objects. An instance member belongs to each object separately. Static fields hold data that is shared, and static methods can be called without creating an object first.

    A static member is declared with the static keyword and used through the class name, such as Math.max(3, 7). Each object has its own copy of an instance field, so changing one object's field does not affect another. A static method has no particular object to work with, so it cannot use this or access instance fields directly. A common pattern is a static counter that each constructor increments to count how many objects were made. A static field exists only once, no matter how many objects are created.

    class Robot {
        static int count = 0;
        String name;
    
        Robot(String name) {
            this.name = name;
            count++;
        }
    }
    
    new Robot("A");
    new Robot("B");
    System.out.println(Robot.count);
    

    Worked example

    Trace the code above.

    1. count is static, so one shared count exists for all Robot objects, and it starts at 0.
    2. new Robot("A") sets its own name to A and increases count to 1.
    3. new Robot("B") sets its own name to B and increases count to 2.
    4. Robot.count prints 2, and each robot keeps its own name.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What number does this Java code print?

      public class Main { public static void main(String[] args) { new Tile(); new Tile(); new Tile(); new Tile(); new Tile(); System.out.println(Tile.count); } }

      class Tile { static int count = 0;

      Tile() { count++; } }

    • Medium

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Token obj1 = new Token(); Token obj2 = new Token(); Token obj3 = new Token(); Token obj4 = new Token(); Token obj5 = new Token(); System.out.println(obj5.id); } }

      class Token { static int count = 0; int id;

      Token() { count++; id = count; } }

    • Hard

      What number does this Java code print?

      public class Main { public static void main(String[] args) { Crate a = new Crate(); Crate b = new Crate(); b.count = 5; System.out.println(a.count * Crate.count); } }

      class Crate { static int count = 0; }