0

part of my programm:

public class Backpack {
    private int maxamount, maxweight;

    public Backpack(int ma, int mw){
    setMaxamount(ma);
    setMaxweight(mw);
}
    public void setMaxamount(int ma){
        this.maxamount=ma;
    }
    public void setMaxweight(int mw){
        this.maxweight=mw;
    }

    Item IA[]=new Item[maxamount];
    int weightA[]=new int[maxamount];
    String nameA[]=new String[maxamount];

    public char addItem(Item I){

    ...

When I use this the variable "maxamount" has no value and I can't store values in the array, but when I'm using this:

public class Backpack {
    private int maxamount, maxweight;

    public Backpack(int ma, int mw){
    setMaxamount(ma);
    setMaxweight(mw);
}
    public void setMaxamount(int ma){
        this.maxamount=ma;
    }
    public void setMaxweight(int mw){
        this.maxweight=mw;
    }

    public char addItem(Item I){
        Item IA[]=new Item[maxamount];
        int weightA[]=new int[maxamount];
        String nameA[]=new String[maxamount];

    ...

,"maxamount" has the value it's supposed to have(from another class), but I need the arrays and the values stored in them in other methods too. So how can I initialize the Arrays out of the method so I can use them for every method?

julian
  • 1

2 Answers2

0

if you do this

Item IA[]=new Item[maxamount];
int weightA[]=new int[maxamount];
String nameA[]=new String[maxamount];

IA, weightA, and nameA will be initialized before you even create an instance of the class Backpack

as RC suggested it is a good practice to place those in the constructor...

Remember a Constructor is there to prepare all the elements you need in the object!

Do it right, do it clean!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

The problem you are having is that Java runs field initializers before it runs the body of the constructor. So when this executes:

Item IA[]=new Item[maxamount];
int weightA[]=new int[maxamount];
String nameA[]=new String[maxamount];

The field maxamount has not yet been assigned. (Another way to think about it—and what the compiler actually does—is that it copies the above initializations to the start of the constructor.)

The solution is to move the initialization into the constructor:

Item IA[];
int weightA[];
String nameA[];

public Backpack(int ma, int mw){
    setMaxamount(ma);
    setMaxweight(mw);
    IA[]=new Item[maxamount];
    weightA[]=new int[maxamount];
    nameA[]=new String[maxamount];
}

By the way, you should not initialize maxamount and maxweight by calling the setter methods from the constructor. At least you shouldn't be doing that if there's any chance the setter methods might be overridden in a subclass. (See, e.g., this thread for why.) Use one of the following approaches:

  • make the setter methods final;
  • make the class final;
  • call private setter methods (e.g., _setMaxamount(ma)) that the public setter methods also use;
  • or set the fields directly in the constructor (my usual approach).

For more complicated situations (for instance, where proper initialization requires calling an overridable method, such as an abstract method), make the constructor(s) private and use static factory methods or the builder pattern to create class instances.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521