when wanting to create a receipt object "non-static variable this can not be referenced from a static context"

3

I have a program that should simulate a card game. The Deck class is responsible for creating the deck and shuffling it (Naipe object). When trying to create the object (only one will be created) I get the error of the title.

This is my main and the Deck class

import java.util.*;

public class Naipes {

public static void main() {

    //instancia de la clase principal para poder crear el objeto deck
    Naipes instancia = new Naipes();

    int cartas;
    int manos;
    Deck deck = new Deck(); //creacion del deck
    ArrayList<Mano> mesa = new ArrayList<Mano>(); // mesa contendra los objetos mano
    Scanner sc = new Scanner(System.in);

    System.out.print("Cantidad de jugadores (manos) que hay en la mesa: ");
    manos = sc.nextInt();
    sc.nextLine();

    System.out.print("\nCantidad de cartas que tendra cada mano: ");
    cartas = sc.nextInt();  

    if ((manos * cartas) > 52) {
        System.out.print("NO HAY SUFICIENTES CARTAS");
        System.exit(1);
    }

    for (int i = 1; i <= manos; i++) {
        mesa.add(new Mano(i, cartas)); // el contador "i" representara el ID de la mano creada

        // loop que asigna los naipes a las manos
        for (int n = 0; n < manos; n++) {
    ((Mano) mesa.get(i-1)).naipes.add(Deck.baraja.remove(0));
        }
    }

}

private class Deck {

ArrayList<Naipe> baraja = new ArrayList<Naipe>();

    public Deck() {
    // lista que almacena los objetos naipe
    crearBaraja(this.baraja);
    barajearDeck(this.baraja);
    }

    private ArrayList<Naipe> crearBaraja(ArrayList baraja) {

        int suit = 0; //el palo del Naipe
        char signo;
        for(int i = 0; i < 5; i++) {

            //loop crea naipes del 0 al 10
            for( int val = 2; val < 11; val++) {
                signo = (char)(val + '0');
                baraja.add(new Naipe(suit, val, signo));
            }

            // se crean naipes J, Q, R, A
            baraja.add(new Naipe(suit, 10, 'J'));
            baraja.add(new Naipe(suit, 10, 'Q'));
            baraja.add(new Naipe(suit, 10, 'R'));
            baraja.add(new Naipe(suit, 11, 'A'));

            suit++;
        }
    }

    private void barajearDeck(ArrayList baraja) {
        Collections.shuffle(baraja);
    }
}

If you need to see the classes Hand and Playing Cards are here

private class Mano {

int ID;
ArrayList<Naipe> naipes = new ArrayList<Naipe>();
int numNaipes;

    public Mano (int identidad, int cantidad_naipes) {

        this.ID = identidad;
        this.numNaipes = cantidad_naipes;
    }

    public int suma() {

        int suma = 0;

        for (int i = 0; i < this.numNaipes; i++) {
            suma += this.naipes.get(i).darValor();
        }
        return suma;
    }

    public void impMano() {

        for (int i = 0; i < this.numNaipes; i++) {
            this.naipes.get(i).mostrar();
        }
    }

}

private class Naipe {

int valor;
char simbolo;
String mazo;

    private Naipe (int palo, int val, char signo) {
        this.valor = val;
        this.simbolo = signo;
        this.mazo = determinarMazo(palo);
    }

    private String determinarMazo(int palo) {

        String suit;

        if (palo == 0)
            suit = "Picas";
        else if (palo == 1)
            suit = "Diamantes";
        else if (palo == 2)
            suit = "Treboles";
        else 
            suit = "Corazones";

        return suit;
    }

    public int darValor() {
        return this.valor;
    }

    public void mostrar() {
        System.out.println(this.simbolo + " de " + this.mazo);
    }
}
    
asked by Luis César 22.03.2018 в 22:27
source

4 answers

0

I think the problem is that you are referencing class Deck when I understand that you wanted to call the object deck instantiated here:

Deck deck = new Deck();

Change this for Deck to deck (and step to try to name variables better to avoid these problems)

 for (int n = 0; n < manos; n++) {
    ((Mano) mesa.get(i-1)).naipes.add(deck.baraja.remove(0));
 }

Also, in the crearBaraja method you need to return the baraja :

 return baraja;
    
answered by 22.03.2018 в 22:52
0

Greetings, Luis César.

What happens is that the class Deck is not a static class, but a private class and you are wanting to access it statically.

Depending on what you want to do, you could simply modify your class Deck of this:

private class Deck

To this:

public static class Deck // también puede ser private static class

Or, you could declare your class Deck in a separate file from Naipes

    
answered by 22.03.2018 в 23:04
0

Well, you may only miss the reserved word static in the functions you created.

Example.

private static ArrayList<Naipe> crearBaraja(ArrayList baraja) {

....
private static void barajearDeck(ArrayList baraja) {

....

And so on everyone.

    
answered by 22.03.2018 в 22:46
-1

You have your classes defined as follows:

public class Naipes {
    ...
    private class Deck {
        ...
    }
}

That way Deck is an inner class of Naipes and can only exist as a reference associated with an instance of Naipes .

In an instance method of Naipes you can do:

Deck d = new Deck();

But in a static method of Naipes you have to specify which is the instance of Naipes that contains Deck:

Naipes n = new Naipes();
Deck d = n.new Deck();

Then, the recommendation for readability in your case is not to use inner classes . Instead you can use static nested class :

public class Naipes {
    ...
    private static class Deck {
        ...
    }
}

Or do not use the same file for all your classes, and use higher level public classes in different files.

You can review more about these classes in the following link (in English)

    
answered by 23.03.2018 в 00:16