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; new creates the object.
  • Assigning one reference to another shares the same object.
  • == compares identity (same reference), not field contents.
  • Two new calls 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);
  }
}

FAQ: Java Objects

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java objects examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java objects in this Java Java lesson (Java Objects).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.