Skip to content

Object

This content is for Java. Switch to the latest version for up-to-date documentation.

An object is an instance of a class, a concrete realization with specific values for the attributes defined in the class which occupies memory.

// Declaration and initialization
Person person = new Person("John", 25);

In Java, variables of reference types don’t contain the actual object but instead hold a reference (memory address) to where the object is stored.

// Creating an object and assigning its reference
String text;
text = new String("Hello");

When an object no longer has any references pointing to it, it becomes eligible for garbage collection:

Person person = new Person("John", 25);
person = new Person("Jane", 28); // Original "John" object is now unreachable
Built with passion by Ngineer Lab