Java Tutorial
Java Objects
A class-type variable holds a reference — an address of an object — not a copy of the object itself. Two names can point at the same instance.
new creates the object
Declaring Player p; only reserves a reference slot. Until you assign new Player() (or another existing object), there is no instance behind the name. new allocates the object and returns a reference to it.
Example
class Box {
int value;
}
public class Main {
public static void main(String[] args) {
Box b = new Box();
b.value = 42;
System.out.println(b.value);
}
}Two names, one object
Assignment between class-type variables copies the reference, not the fields. AfterBox b2 = b1;, both names point at the same heap object. Change one and you see it through the other.
Example
class Box {
int value;
}
public class Main {
public static void main(String[] args) {
Box b1 = new Box();
b1.value = 1;
Box b2 = b1; // same object
b2.value = 99;
System.out.println(b1.value); // 99
System.out.println(b1 == b2); // true — same identity
}
}== on objects asks "same identity?" — same reference. For equal contents (two different boxes both holding 99), you later use equals. For now, know that == is about the reference.
Separate objects stay separate
Call new twice and you get two independent objects. Changing one leaves the other alone — that is the contrast with shared references.
Example
class Box {
int value;
}
public class Main {
public static void main(String[] args) {
Box a = new Box();
Box b = new Box();
a.value = 1;
b.value = 2;
System.out.println(a.value + " " + b.value); // 1 2
System.out.println(a == b); // false
}
}Try It Yourself
Exercise: Create one Counter with field int n. Assign a second variable to the same reference, increment through the second name, and print through the first. What do you see?
Show solution
class Counter {
int n;
}
public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
c1.n = 0;
Counter c2 = c1;
c2.n++;
System.out.println(c1.n); // 1 — same object
}
}Both variables share one object, so the increment is visible through either name.
Key Takeaways
- Class-type variables hold references;
newcreates the object. - Assigning one reference to another shares the same object.
==compares identity (same reference), not field contents.- Two
newcalls mean two objects with independent fields.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Engineering
Two references, one sensor
Assigning Sensor b = a copies the reference, not a second device. Changing a field through either name hits the same object.
Example
class Sensor {
double reading;
}
public class Main {
public static void main(String[] args) {
Sensor a = new Sensor();
a.reading = 1.2;
Sensor b = a;
b.reading = 1.5;
System.out.println(a.reading);
}
}