Classes and Objects in Java - Quiz
Total: 5 questions
1. What is the difference between a class and an object in Java?
What is the difference between a class and an object in Java?
A class is a blueprint (a description, a data type): it lists the fields and methods and already exists when you write and compile the code. An object is a concrete instance built from that blueprint with the new operator at run time. The class description is loaded by the JVM once, while every object occupies its own block on the heap and keeps its own copy of the instance fields. One class can produce any number of objects, each holding different values: class Box { double width; } is the class, Box myBox = new Box(); is an object. An everyday analogy: the class is the architectural drawing of a house, the object is a house actually built from it.
2. How do you create an object in Java, and what values do its fields hold right after creation?
How do you create an object in Java, and what values do its fields hold right after creation?
Creating an object is a two-step process. First you declare a variable of the class type (Box myBox;) — it does not hold an object yet, it can only refer to one. Then the new operator allocates memory for the object on the heap and returns a reference to it: myBox = new Box();. Both steps are usually written on one line: Box myBox = new Box();. The parentheses after the class name are a constructor call; if the class declares no constructor, the compiler adds a no-argument default constructor. Right after creation every field gets the default value for its type: 0 (or 0.0) for numeric types, false for boolean, null for reference types. Note that default values apply to instance fields and static fields only — local variables inside a method are never initialised automatically, and reading one before assignment is the compile-time error "variable might not have been initialized".
3. What happens when you write Box b2 = b1; — is a new object created?
What happens when you write Box b2 = b1; — is a new object created?
No. The line Box b2 = b1; copies the reference, not the object: no second box is created and both variables point at the same object on the heap. A change made through either variable is therefore visible through both:
Box b1 = new Box();
Box b2 = b1;
b1.width = 10;
b2.width = 3;
System.out.println(b1.width); // 3.0
System.out.println(b2.width); // 3.0
To get an independent copy you have to create a new object with new and copy the field values across (or clone the object).
4. Which methods does an object of any class have, and where do they come from?
Which methods does an object of any class have, and where do they come from?
Every class in Java implicitly inherits from java.lang.Object: the compiler reads class Box {} as class Box extends Object {}. That is why even an object of an empty class already exposes the methods of Object: toString() — the string representation, returning something like Box@1b6d3586 (class name plus hash code in hexadecimal) by default; equals(Object obj) — object comparison, which compares references by default and therefore behaves exactly like ==; hashCode() — an integer hash code used by HashMap and HashSet; getClass() — returns a Class object describing the runtime type; plus clone(), wait(), notify() and notifyAll(). In real classes toString(), equals() and hashCode() are almost always overridden so that objects print readably and compare by value rather than by reference.
5. What mistakes do beginners most often make when working with classes and objects in Java?
What mistakes do beginners most often make when working with classes and objects in Java?
Five typical mistakes. 1. Touching a field before the object exists: Box myBox; declares a reference only, so myBox.width = 10; without new gives a compile error about an uninitialised variable, or a NullPointerException at run time if the reference was set to null. 2. Comparing objects with ==: it compares references, not contents, so two distinct objects holding identical values still produce false; use an overridden equals() for value comparison. 3. Confusing a copied reference with a copied object: Box b2 = b1; does not create a second box, both names point at the same one. 4. Calling a non-static method directly from main: getVolume() belongs to an object and must be called as myBox.getVolume(), otherwise you get "non-static method cannot be referenced from a static context". 5. The file name does not match the public class name: public class Box has to live in Box.java.