Say for example I have a class called Phone.
What is the difference between:
Phone p;
and
Phone p = new Phone(200) //(200 is the price of the phone).
and
new Phone(200)
I’ve googled and even tried it on Eclipse but can’t figure it out.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Phone p;only declares a reference handlerpwhich 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 handlerpwhich points to a newly createdPhoneobject (initialized withPhone(200)).new Phone(200)creates a newPhoneobject, 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
Phoneis a reference-type (it’s aclass),Phone pis always a “reference to aPhone“.)