简体   繁体   中英

Java can't save my input. See my code

class History  {

    public String[] history;

    public History(String[] history) {
       if (history == null)
           history = new String[]{};
       else 
           history = this.history
    }

}

It just keep saving null down no matter what i give it.. So can't work with it..

Don't you mean this?

else this.history = history;

You also have a mistake in this line:

history = new String[]{};

It should be:

this.history = new String[]{};

When I program, I never give local variables the same name as class variables. It only leads to confusion.

Use this.history to refer to the class variable and history to refer to the method argument.

You have these being confused, and it's not setting the member variable properly. When you refer to history , this means the argument to the method, not the member variable.

public History(String[] history)
{
    if (history == null)
        this.history = new String[]{};
    else
        this.history = history;
}

For assignment, use

this.history = history

instead. I think there is a naming conflict between the local variable and the object attribute.

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