简体   繁体   中英

How do you assign values to an ArrayList member variable of a Java Class during build method?

I have a class with an ArrayList as a member variable

public class Tile {
     private final String Name;
     private final ArrayList<Integer> sides;

     public Tile (String name, int up, int right,int down, int left){

        this.Name = name;
        this.sides.add(up);

    }
}

So when I create a Tile object I want to set the up, right, down, left values into a 4 object array for the Tile Class. With the above code I'm getting an error from NetBean "variable sides might not have been initialized". I'm not sure what that means, because I'm attempting to initialize it with the build method.

Looking for help from NetBean ended up with the following, which gets the same error.

this.sides.set(0, up);

I'm new to Java so if someone could provide some I'm looking for some instruction on the concepts I'm failing to understand.

Thank you

You got the message variable sides might not have been initialized which explained your problem already. You have to initialize the sides array list first.

Like this:

private final ArrayList<Integer> sides = new ArrayList<>();

Or within the constructor before you add objects to your sides list, like this:

public Tile (String name, int up, int right,int down, int left){
    sides = new ArrayList<Integer>();
    this.Name = name;
    this.sides.add(up);
}

The solution :

Modify:

 private final ArrayList<Integer> sides;

To

 private final ArrayList<Integer> sides = new ArrayList<>();

The reason :

Java does not automatically initialize variables for you, sides is still uninitialized (like null ) when you are trying to .add stuff to it.

You may notice that you don't always have to initialize variables upon construction/declaration, but you've done two things that requires you to initialize sides in your code:

  1. You have the final keyword on side Variables with the final keyword ensures that they would only be assigned exactly once during construction/declaration . Failing to do this would result in a compilation error: " variable yourVarNameHere might not have been initialized "

  2. You are using side in the constructor This is more of a semantic issue. You would notice that the error goes away if you remove the final keyword, but nonetheless would run into a NullPointerException when executing the program. The reason to this is exactly the reason in my first paragraph: sides is still uninitialized (like null ) when you are trying to .add stuff to it.

您需要为sides对象初始化:

 private final ArrayList<Integer> sides = new ArrayList<Integer>();

The reason its failing is that you have never initialized sides list.

This is basically advantage of having getters or init blocks. I can see such questions at couple of places, so just listing down all the better places to initialize such variables:

1. Getter

So instead of calling variable direct with

this.sides.add(up);

you can have a getter for sides like this

public getSides(){
if(this.sides == null){
this.sides = new ArrayList<Integer>();
}
//you can also add even more validations here before anyone access it
return this.getSides();
}

and accordingly you can add values like this.getSides().add(up);

2. Init Blocks

Init blocks are the one that is called before every constructor. So in case you have multiple overloaded constructors. You can have only one init block like this:

{
this.sides = new ArrayList<Integer>();
}

3. Define while Declaring

You can also define such variables at the time of declaration itself like below:

private final ArrayList<Integer> sides = new ArrayList<Integer>();

4. Constructor

Not recommended in case of multiple constructors but still you can define it in each Constructor like this

     public Tile (String name, int up, int right,int down, int left){
        this.sides = new ArrayList<Integer>();
        this.Name = name;
        this.sides.add(up);

    }

5. Init Method

You can also create your own init method to initalize all such variables and call it in every overloaded constructor like below:

public void init() {
this.sides = new ArrayList<Integer>();
}

and then call this function either in init block or constructor.

Hope that helps you.

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