Difference between creating a “new object” and “Class objectname”

后端 未结 5 557
忘掉有多难
忘掉有多难 2021-01-06 15:03

Say for example I have a class called Phone.

What is the difference between:

Phone p;

and

Phone p = new Phone(200)          


        
相关标签:
5条回答
  • 2021-01-06 15:22

    http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

    0 讨论(0)
  • 2021-01-06 15:31
    Phone p;
    

    is just a reference or a "pointer" as some people liking C language would call it. It contains the path to an object but currently the path is null.

    Phone p = new Phone(200) //(200 is the price of the phone).
    

    Here you are creating a new Phone object, by calling its constructor which takes the 200 value. The object is then assigned to the reference p.

    new Phone(200)
    

    Here you are just creating an Object of type Phone but do not have a reference to it, so this object is in turn to be Garbage Collected by the JVM (if not referenced to something else internally).

    Regards!

    0 讨论(0)
  • 2021-01-06 15:36

    The first snippet only declares a variable p with a given type Phone. You have not created any object, only a variable.

    The third instantiates a new Phone object (which is not assigned to a variable).

    The second combines the two, the declaration and instantiation, into a single line of code.

    Phone p; // declaration of variable 
    p = new Phone(200); // instantiation of object, assigned to variable 
    
    Phone p = new Phone(200); // declaration and instantiation in a single line
    

    For p to be used, you need to instantiate it (or otherwise initialize it). The declaration itself is not useful. In a local (a variable declared inside the scope of a method), it is illegal to use it without first initializing it. If it's a class-level member, then it will simply be a null pointer when you try to access any of its member functions.

    0 讨论(0)
  • 2021-01-06 15:40

    Phone p; only declares a reference handler p which doesn't point anywhere (it is a not initialized and cannot be used until you assign something to it [thanks @Anthony]).

    Phone p = new Phone(200); declares a reference handler p which points to a newly created Phone object (initialized with Phone(200)).

    new Phone(200) creates a new Phone object, but since no reference to it is stored anywhere, it becomes immediately eligible for garbage collection (unless its constructor stores a reference somewhere, that is).

    (Note that in Java, all "variables" whose type is a reference-type are really reference handlers. Only variables of value-type contain values directly. Since Phone is a reference-type (it's a class), Phone p is always a "reference to a Phone".)

    0 讨论(0)
  • 2021-01-06 15:43

    Phone p is a reference to a Phone object which has not been initialised.

    Phone p = new Phone(200) is a reference to a Phone object which has been initised with the constructor Phone(int var).

    new Phone(200) creates a new Phone object with the constructor Phone(int var).

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