The difference between Classes, Objects, and Insta

2020-01-22 11:54发布

What is a class, an object and an instance in Java?

16条回答
【Aperson】
2楼-- · 2020-01-22 12:17

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.

查看更多
干净又极端
3楼-- · 2020-01-22 12:18

Class : Structure

Object : Physical Manifestation

Instance : each object created from class

Reference : Address of Object

查看更多
时光不老,我们不散
4楼-- · 2020-01-22 12:20

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:

public class Car {

    int currentSpeed;
    String name;

    public void accelerate() {  
    }

    public void park() {
    }

    public void printCurrentSpeed() {
    }
}

You can create instances of the object Car like this:

Car audi = new Car();
Car toyota = new Car();

I have taken the example from this tutorial

查看更多
姐就是有狂的资本
5楼-- · 2020-01-22 12:23

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.

查看更多
迷人小祖宗
6楼-- · 2020-01-22 12:26

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.

查看更多
爷的心禁止访问
7楼-- · 2020-01-22 12:26

I like Jesper's explanation in layman terms

By improvising examples from Jesper's answer,

class House {
// blue print for House Objects
}

class Car {
// blue print for Instances of Class Car 
}

House myHouse = new House();
Car myCar = new Car();

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"

查看更多
登录 后发表回答