简体   繁体   中英

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

So I'm working on with two class now and just learned to do this, yet still don't know why is this possible and what this is called. Class variable? Objects creating objects? And in the setter method, why the default value is null? is it like String? 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.

There are two classes named CashRegister and RetailItem.

Here goes the instance 'RetailItem item' from CashRegister with a setter.


public class CashRegister
{
    private RetailItem item;

    public void setItem (RetailItem newItem)
    {
        if (newItem != null)
        {
            item = newItem;
        }else{
            item = new RetailItem();
        }
    }
}

RetailItem() is a default constructor from RetailItem class. What does item = new RetailItem(); mean? I don't know where am I even going to start studying. What am I missing?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM