Why do not you show me the stats of the constructor?

0
package Stats;

public class Players {
    private String name;
    private int RIT;
    private int TIR;
    private int PAS;
    private int REG;
    private int DEF;
    private int FIS;
    private int MED;

    public Players (String name, int RIT, int TIR, int PAS, int REG, 
    int DEF, int FIS, int MED) {
        this.name= name;
        this.RIT = RIT;
        this.TIR = TIR;
        this.PAS = PAS;
        this.REG = REG;
        this.DEF = DEF;
        this.FIS = FIS;
        MEDIA = MED;
    }

    public static void main (String[] args) {
        Players player1 = new Players ("aa", 5,3,7,1,7,9,8);
        System.out.println(player1);
    }
}

I'm trying to get all the variables from the constructor with the println but only prints "Stats.Players@14c3d7e" and if I put println (name + "\ n" + RIT + ....); He says that you can not because he is not static. What can I do?

    
asked by Perico 10.11.2018 в 22:52
source

1 answer

0

The problem is that you can not print an object, and the properties can not be read directly from main because it is static, and the properties are members of the object.

To do something like what you need, you could do it like this:

public static void main (String[] args) {
    Players player1 = new Players ("aa", 5,3,7,1,7,9,8);
    System.out.println(player1.name + "\n" + player1.RIT + ....);
}

This way you will print the properties of the newly created object.

Good luck!

    
answered by 10.11.2018 в 23:08