The difference between Classes, Objects, and Instances

前端 未结 16 2168
耶瑟儿~
耶瑟儿~ 2020-11-22 05:32

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

16条回答
  •  野的像风
    2020-11-22 06: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.

提交回复
热议问题