What is a class, an object and an instance in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
A class is a blueprint that is needed to make an object(= instance).
The difference between an object and an instance is, an object is a thing and an instance is a relation.
In other words, instance describes the relation of an object to the class that the object was made from.
Class : Structure
Object : Physical Manifestation
Instance : each object created from class
Reference : Address of Object
The concept behind classes and objects is to encapsulate logic into single programming unit. Classes are the blueprints of which objects are created.
Here an example of a class representing a Car:
You can create instances of the object Car like this:
I have taken the example from this tutorial
The definition "Object is an instance of a class", is conceptually wrong, but correct as per implementation. Actually the object oriented features are taken from the real life, for focusing the mind of programmer from more to less. In real life classes are designed to manage the object.For eg- we human beings have a caste, religion,nationality and much more. These casts, religion, nationality are the classes and have no existence without human beings. But in implementation there is no existence of objects without classes. Object- Object is an discrete entity having some well defined attribute. Here discrete mean something that makes it unique from other. Well defined attribute make sense in some context. Class- Classification of object having some common behaviour or objects of some common type.
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.
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.
I like Jesper's explanation in layman terms
By improvising examples from Jesper's answer,
myHouse and myCar are objects
myHouse is an instance of House (relates Object-myHouse to its Class-House) myCar is an instance of Car
in short
"myHouse is an instance of Class House" which is same as saying "myHouse is an Object of type House"