“Object” vs “Object Variable” in Java?

后端 未结 6 1566
春和景丽
春和景丽 2021-02-01 19:01

I am teaching myself Java, and one of the review exercises in my book asks for the difference between an \"object\" and an \"object variable.\"

I know what an object is

相关标签:
6条回答
  • 2021-02-01 19:32

    tl;dr;fmoe

    I came across the same question in a review exercise found within Cay Horstmann, Big Java, Early Objects (John Wiley & Sons, 2013), 73.

    Cay defines an object variable as a variable (whose type is a class) which stores a reference to the memory location of an object.

        Rectangle box = new Rectangle();
    
    • box is the object variable which stores a reference to the newly instantiated Rectangle object's memory location.

    Multiple object variables can exist which store the same reference to an object's memory location.

        Rectangle box = new Rectangle(5,10,20,30);
        Rectangle box2 = box;
    

    Calling a mutator/mutagenic method (modifies the object on which the method is invoked) on either object variable mutates (modifies) the object since the object variables reference the same object's memory location

        box2.translate(15, 25); 
        System.out.println(box.getY()); // 35.0
        System.out.println(box2.getY()); // 35.0
    

    This gets a bit confusing if you compare Cay's definition with the information from The Java Tutorials, and numerous other sources, but I believe this is the answer to your question when placed within the context of Cay's book(s).

    I prefer this perspective => An object's memory location is stored in the object reference (object variable per Cay). When I invoke a method on an object, I specify the object reference followed by the dot (.) operator, followed by the method name.

        objectReference.method();
    

    If the public interface of a class allows access to one or more of it's instantiated object's fields (AKA instance variables, object variables, class member variables, properties, or attributes... depending on programming language and documentation) then I can access it by specifying the object reference, followed by the dot (.) operator, followed by the field name

        objectReference.fieldName;
    

    Why do I prefer this? I'm not a fan of using variable as a noun too many times; overloaded semantics boggle my simple mind.

    0 讨论(0)
  • 2021-02-01 19:47

    At university its tought, that an Object Variable is: "a variable whose type is a class, and which holds a reference to an object that is an instance of that class"

    Object thing = new Object();
    
    0 讨论(0)
  • 2021-02-01 19:48

    I'll bite.

    The Object is the instance itself, whereas the Object Variable is the reference to the Object.

    Here's a contrived example:

    Object o = new Object();
    Object ref1 = o;
    

    In his case, there is a single instance of the Object, but it is referenced by two Object Variables: o and ref1.

    When an Object is no longer referenced by an Object Variable, the Object is garbage collected.

    0 讨论(0)
  • 2021-02-01 19:56

    @Faaris Cervon : Object variables are the variables which does not create any object but refer to some object .. exmp : Date d1= new Date();

    d1 is a object.

    date d2; d2 is not an object and neither it refers any object But it can hold any object of type date. d2=d1; // valid.

    hence d2 is a object variable. It is important to remember that object variables does not contains objects actually but it refers to some object.

    0 讨论(0)
  • 2021-02-01 19:56

    I found something that help you too. There are few words like object, object variable and object reference.

    Object variable and object reference are similar in the way that object variable stores the reference of an object where as object reference describes the location of an object. (You might be confused here so just consider them as same)

    consider an example::

    Class Car{
    ....
    ....
    }

    Car AudiQ7; //S1
    Car AudiQ8 = new Car(); //S2

    Here in S1 we created only object. Means it does not refer to memory.

    In S2 we created an object variable/reference means it refers to memory location.

    0 讨论(0)
  • 2021-02-01 19:57

    It's a synonym of "instance variable":

    class A {
        static int m;  // <-- class variable
        int n;         // <-- instance variable
        ...
    }
    

    Evidently, this term is not so commonly used, and it would better to avoid any potential ambiguities by just sticking with "instance variable".

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