The difference between Classes, Objects, and Instances

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

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

相关标签:
16条回答
  • Class is Data Type,You use this type to create object.

    • Instance is Logical but object is Physical means occupies some memory.

    • We can create an instance for abstract class as well as for interface, but we cannot create an
      object for those.

    • Object is instance of class and instance means representative of class i.e object.

    • Instance refers to Reference of an object.

    • Object is actually pointing to memory address of that instance.

    • You can’t pass instance over the layers but you can pass the object over the layers

    • You can’t store an instance but you can store an object

    • A single object can have more than one instance.

    • Instance will have the both class definition and the object definition where as in object it will have only the object definition.

    Syntax of Object:

     Classname var=new Classname();
    

    But for instance creation it returns only a pointer refering to an object, syntax is :

     Classname varname;
    
    0 讨论(0)
  • 2020-11-22 06:11

    In java, the objects are spawned on heap memory. These require reference to be pointed and used in our application. The reference has the memory location of the object with which we can use the objects in our application. A reference in short is nothing but a name of the variable which stores the address of the object instantiated on a memory location.

    An instance is a general term for object. FYI, Object is a class.

    For Example,

    Class A{
    
    }
    
    A ref = new A();
    

    For the above code snippet, ref is the reference for an object of class A generated on heap.

    0 讨论(0)
  • 2020-11-22 06:14

    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.

    0 讨论(0)
  • 2020-11-22 06:19

    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.

    0 讨论(0)
  • 2020-11-22 06:19

    While the above answers are correct, another way of thinking about Classes and Objects would be to use real world examples: A class named Animal might contain objects like Cat, Dog or Fish. An object with a title of Bible would be of class Book, etc. Classes are general, objects are specific. This thought example helped me when I was learning Java.

    0 讨论(0)
  • 2020-11-22 06:20

    Honestly, I feel more comfortable with Alfred blog definitions:

    Object: real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). Car have state (current speed, current gear) and behavior (applying brake, changing gear). Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields and exposes its behavior through methods.

    Class: is a “template” / “blueprint” that is used to create objects. Basically, a class will consists of field, static field, method, static method and constructor. Field is used to hold the state of the class (eg: name of Student object). Method is used to represent the behavior of the class (eg: how a Student object going to stand-up). Constructor is used to create a new Instance of the Class.

    Instance: An instance is a unique copy of a Class that representing an Object. When a new instance of a class is created, the JVM will allocate a room of memory for that class instance.

    Given the next example:

    public class Person {
        private int id;
        private String name;
        private int age;
    
        public Person (int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + id;
            return result;
        }
    
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person other = (Person) obj;
            if (id != other.id)
                return false;
            return true;
        }
    
        public static void main(String[] args) {
            //case 1
            Person p1 = new Person(1, "Carlos", 20);
            Person p2 = new Person(1, "Carlos", 20);
    
            //case 2
            Person p3 = new Person(2, "John", 15);
            Person p4 = new Person(3, "Mary", 17);
        }
    }
    

    For case 1, there are two instances of the class Person, but both instances represent the same object.

    For case 2, there are two instances of the class Person, but each instance represent a different object.

    So class, object and instance are different things. Object and instance are not synonyms as is suggested in the answer selected as right answer.

    0 讨论(0)
提交回复
热议问题