The difference between Classes, Objects, and Instances
What is a class, an object and an instance in Java?
A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.
This is easy to understand if you look at an example. For example, suppose you have a class House
. Your own house is an object and is an instance of class House
. Your sister's house is another object (another instance of class House
).
// Class House describes what a house is
class House {
// ...
}
// You can use class House to create objects (instances of class House)
House myHouse = new House();
House sistersHouse = new House();
The class House
describes the concept of what a house is, and there are specific, concrete houses which are objects and instances of class House
.
Note: This is exactly the same in Java as in all object oriented programming languages.
Java (and any other programming language) is modeled in terms of types and values. At the theoretical level, a value is a representation for some quantum of information, and a type is a set of values. When we say value X is an instance of type Y, we are simply saying that X is a member of the set of values that is the type Y.
So that's what the term "instance" really means: it describes a relationship not a thing.
The type system of the Java programming language supports two kinds of types, primitive types and reference types. The reference types are further divided into the classes and array types. A Java object is an instance of a reference type.
An object is a class instance or an array. (JLS 4.3.1)
That's the type theoretic view.
In practice, most Java developers treat the words "instance" and "object" as synonyms. (And that includes me then I'm trying to explain something quickly.) And most developers use the word "value" rather than "instance" to refer to an instance of a primitive type.