Java - Object (instance of a class)

Java Conceptuel Diagram


An object:

  • stores its state in fields
  • and exposes its behaviour through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

From a class, you can create several object (instance of a class) with different state but the same behaviours. A class provides the blueprint for objects; you create an object from a class. When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables.

A typical Java program creates many objects, which as you know, interact by invoking methods.

The phrase “instantiating a class” means the same thing as “creating an object.” When you create an object, you are creating an “instance” of a class, therefore “instantiating” a class.


Bundling code into individual software objects provides a number of benefits, including:

  • 1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
  • 2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
  • 3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
  • 4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Data Type

Every object is either:


For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class.




The Java Machine has an internal id for each object created.

To verify that they are the same object use the == operator.

Debugger may also show an internal integer id such as 3128 or 3129 but this is an internal representation of their implementation. See ObjectReference uniqueID


Two objects may have different data but may be considered logically equals.

This happens with the help of a hash function.

There is two level of hash:

  • The default hash function on the system level


(Create|Instantiate) an object

Each of the following statements creates an object and assigns it to a variable:

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

Each of these statements has three parts (discussed in detail below):

  • 1. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
  • 2. Instantiation: The new keyword is a Java operator that creates the object.
  • 3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

The new operator

Instantiating a Class

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

The phrase “instantiating a class” means the same thing as “creating an object.” When you create an object, you are creating an “instance” of a class, therefore “instantiating” a class.

The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate.

The return value

The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:

int height = new Rectangle().height;


Interfaces and abstract classes can never be instantiated. You must always instantiate a concrete class but you can assign the resulting object to an interface.

Example where:

with this hierarchy

InterfaceFoo < AbstractFoo < ImplementationFoo
// Valid
InterfaceFoo foo= new ImplementationFoo ();
AbstractFoo foo = new ImplementationFoo ();
// Invalid
InterfaceFoo foo = new InterfaceFoo();
InterfaceFoo foo = new AbstractFoo();
AbstractFoo foo = new AbstractFoo();


For a class with a constructor without argument otherwise others reflection method must be used

Foo foo;
String fooClassName = getFooImplClass();
InterfaceFoo  foo =  (InterfaceFoo) Class.forName(fooClassName).newInstance();

Builder Pattern


instanceof (Type)

Object o = new Integer("3");

if (o instanceof Integer)) {
        System.out.println("This is an integer");

Drop an object

Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed. Managing memory explicitly is tedious and error-prone.

The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is drop:

  • by the garbage collection when there are no more references to that object.
  • explicitly by setting the variable to the special value null.

Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.


  • The == operator verify that they are the same object. (Ie object id not the same value !)
  • whereas the equals method verify that you have the same logical object (ie the same passport Id for instance)

See What does the equals method in Java?



public String toString()
	return toStringHelper(this)
			.add("tableName", tableName)
			.add("type", type)
			.add("columns", columns)

This (Object access)

this is a reference pointer to the object created by the class.

In an inner class, you can access the outer object with the qualified this - ie OuterClass.this from the InnerClass

Documentation / Reference

Discover More
Card Puncher Data Processing
JPA - (Object) Persistence Provider Library

A persistence provider refers to an implementation of the Java Persistence API. The persistence provider is a library that provides the functionality to persist objects in the application. EclipseLink...
Jconsole Memory Chart
JVM - Memory (The Heap)

The JVM memory consists of the following segments: Heap Memory, which is the storage for Java objects Non-Heap Memory, which is used by Java to store loaded classes and other meta-data JVM code...
Java Conceptuel Diagram

Why has become so popular among application developers? Primarily because makes application developers more productive. It is a modern, robust, object-oriented language. ’s unprecedented popularity...
Java Conceptuel Diagram
Java - (Data Type|Type)

data type in the java world. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. The language supports four kinds...
Java Conceptuel Diagram
Java - (Environment|Operating System)

Most of the OS informations are managed in the java/lang/SystemSystem class. Every Java application has a single instance of java/lang/Runtimeclass Runtime that allows the application to interface with...
Java Conceptuel Diagram
Java - (Method|Functions)

A function that belong to an object is called a methods. A static method is then a sort of function. Methods: operate on an object's internal state and serve as the primary mechanism for object-to-object...
Java Conceptuel Diagram
Java - Abstract Modifier

Abstract is a modifier that require override. An abstract class is a class that can't be instantiated. It's only purpose to be extended by other classes. See enum Abstract class provides a skeletal...
Java Conceptuel Diagram
Java - Application

An application in shipped in an archive format with a main class that contains a main method that starts the application. instancejava/lang/Runtimeclass RuntimeOperating System environment ...
Simple Class
Java - Class (Definition)

A java/lang/Classclass provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class. A class declaration names the class and encloses the class...
Java Constructor Eclipse
Java - Constructor

in Java. A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that: they use the name of the...

Share this page:
Follow us:
Task Runner