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

后端 未结 5 558
忘掉有多难
忘掉有多难 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: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).

提交回复
热议问题