-3

I have a class names Packers with an array names Wins. I need to take an integer that I get from the main program and put it into the array. I have getters and setters I am just not sure how it works. Fairly new at java.

    //getters and setters
    public void setWins(int [] a){
        wins = a;
     }
    public int[] getWins(){
        return wins;
     }
jmf24
  • 11
  • 2
  • 2
    Possible duplicate of [How do getters and setters work?](https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – vinS Dec 15 '17 at 05:13
  • The above looks perfect — for setting and retrieving the `wins` array. But I’m guessing that is not what you want to do. A little more detail Is required. – AJNeufeld Dec 15 '17 at 05:13
  • in your code you have method to pass entire array. – Sangam Belose Dec 15 '17 at 05:16

2 Answers2

0

Create an instance of the the object new Packers(), then call the getters and setters:

Packers variableName = new Packers();
variableName.setWins(new int[]{1,2,3,4,5});
System.out.println(Arrays.toString(variableName.getWins()));
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
0
public class packers{

public int [] wins = new int[5];

    public int[] getWins() {
        return wins;
    }

    public void setWins(int[] wins) {
        this.wins = wins;
    }

}       

public class Test{
public static void main(String[] args) {
         Packers p = new Packers();
         // to pass single element to an existing array
         p.getWins()[index] = 2;

         // or you can create entire array and just pass it to setWins method.
          p.setWins(new int[]{2, 4, 6});
         }
}

in the first approach you are setting the value on the existing array. In second approach you are pointing reference wins --> to the newly created array which you have passed.

just read basics about pass by value and reference.

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48