Back

Activity 4.6 Object References and Default Values

divider

Introduction

Activity 4.6

Object References and Default Values

Topics

  • Default initialization of instance variables
  • Primitive vs object defaults
  • What null means
  • Object references
  • Why NullPointerException occurs

Default Initialization

Instance variables are automatically assigned default values before the constructor runs.

class Example
{
private int count;
private double price;
private boolean active;
private String name;
private Product featuredProduct;
}

Default Values

  • int → 0
  • double → 0.0
  • boolean → false
  • char → '\u0000'
  • Any object → null

Constructors Run After Defaults

Default values are assigned before the constructor executes.

class Example
{
private int count;
private String name;
public Example()
{
// Constructor runs AFTER default initialization
name = "Default";
}
}

What Does null Mean?

null means a variable does not reference any object.

  • null is not an object
  • null is only used with object variables
  • null is different from an empty String

null vs Empty String

String a = null;
String b = "";
System.out.println(a);
System.out.println(b.length());

NullPointerException

If a variable is null, there is no object to use.

Product p = null;
p.applyDiscount(10); // NullPointerException

Preventing NullPointerException

if (p != null) {
p.applyDiscount(10);
}

Object References

Object variables store references to objects, not the objects themselves.

Dog dog1 = new Dog("Fido", "Greyhound", 3, "gray");
Dog dog2 = dog1;

Why This Matters

  • Many fields start as null by default
  • Constructors often prevent null problems
  • Composition (next lesson) relies heavily on object references
  • Data structures (next module) are also reference types

'F' → Fullscreen

Objectives

  • Explain default values of instance variables
  • Describe what null represents
  • Identify object reference behavior
  • Prevent NullPointerException using null checks
divider

Activity Tasks

Task 1: Predict the Defaults

Examine the class below. Write down the default value for each instance variable before the constructor runs.

Demo.java
class Demo {
private int number;
private boolean flag;
private String text;
}

Task 2: Safe Use of null

Run the program below and explain why it does not crash.

Test.java
class Test {
public static void main(String[] args) {
String name = null;
if (name != null) {
System.out.println(name.length());
} else {
System.out.println("Name is null");
}
}
}
divider

Sample Output

Sample Output
Name is null
divider

Reflection Questions

  1. Why do object variables default to null?
  2. What is the difference between a variable being null and an object being empty?
  3. Why can two variables refer to the same object?
  4. What causes a NullPointerException?
  5. Why do constructors often explicitly initialize object fields?
divider

Submission

Submit your activity responses and reflection answers to the appropriate dropbox.

Activity Complete