[Solved] What do you call this object in Java and why do you use?


I have some trouble understanding your English, so I might have misinterpreted some of your questions.

What do you call this object in Java and why do you use?

I don’t know which “object” you are referring to.

… yet still don’t know why is this possible and what this is called.

It is just called this. There is no other term for it.

Class variable?

No. this is not a class variable.

Objects creating objects?

(Huh?) No. this is not “objects creating objects”.

And in the setter method, why the default value is null?

Because that is the way that Java defined. Any instance variable (like item) whose type is a reference type has a default initial value of null.

(Why? Because that is the only value that makes sense from a language perspective. Anything else, and there would need to be some way for the programmer to say what the default is. Compilers can’t read your mind!)

is it like String?

Not sure what you mean. But if you are asking if you would get the same default value behavior if item had been declared with type String, then Yes.

From what I’m assuming, RetailItem item is like it combined the whole RetailItem class and creating a new variable in another class with its feature.

Not exactly. What RetailItem item is actually doing is declaring a variable in which you may then put a reference to a RealItem object. This variable has the default value null …. until some other value is assigned to it.

What does item = new RetailItem(); mean?

That means create (construct) a new RetailItem instance (an object), and then assign its reference to the variable item.

I don’t know where am I even going to start studying.

I recommend that you start with a good introductory Java textbook, or the Oracle Java Tutorial, or your course lecture notes. Ask your teachers.

But keep at it. If you work at it, you will get to the point where it all starts to make sense. Because, basic Java is a simple and consistent language. The language only gets complicated when you learn about generics, type inference / lambdas and …. multi-threading.

2

solved What do you call this object in Java and why do you use?