Error creating objects

1

Good evening, I was commissioned to carry out a simple program in the POO class in which it consists of doing a simple program in which it consists of a Bank where you can enter or withdraw money. I did it in 2 classes, but in the second class it does not let me declare private objects, it only helps me if I create the objects within the main method and without the private.

Class 1:

public class Cuenta{
    private String titular;
    private double saldo;

    public Cuenta(String nombre, double dinero){
        titular = nombre;
        saldo = dinero;
    }
    public void ingresar(double dinero){
        saldo = saldo + dinero;
    }
    public void retirar(double dinero){
        if(dinero<=saldo)
            saldo = saldo-dinero;
    }
    public double checar(double dinero){
        return saldo;
    }
}

Class 2:

public class Banco{
    private Cuenta cuenta1 = new Cuenta("Carlos Carrete", 2500);
    private Cuenta cuenta2 = new Cuenta("Daniela Pabon", 200);

    public static void main (String [] args){
        cuenta1.ingresar(200);
        cuenta2.ingresar(100);

        cuenta1.retirar(100);
    }
}

Class 2 shows "non static variable can not be referend from static context"

    
asked by Carlos Carrete 27.08.2017 в 05:31
source

2 answers

1

Yes, the error is so because from static methods you can only reference other static methods or static variables. What you must do is create the object within the static method and thus be able to make use of its attributes

public class Banco{
    public static void main (String [] args){
        Cuenta cuenta1 = new Cuenta("Carlos Carrete", 2500);
        Cuenta cuenta2 = new Cuenta("Daniela Pabon", 200); 
        cuenta1.ingresar(200);
        cuenta2.ingresar(100);

        cuenta1.retirar(100);
    }
}

If you want to do with objects, you can do it like this

public class Banco{
  public Cuenta cuenta1 = new Cuenta("Carlos Carrete", 2500);
  public Cuenta cuenta2 = new Cuenta("Daniela Pabon", 200);

  public static void main (String [] args){
    Banco banco = new Banco();
    banco.cuenta1.ingresar(200);
    banco.cuenta2.ingresar(100);

    banco.cuenta1.retirar(100);
  }
}
    
answered by 27.08.2017 в 05:37
0

The methods used are not defined as static within the class Cuenta ,

public class Cuenta{
    private String titular;
    private double saldo;

    public Cuenta(String nombre, double dinero){
        titular = nombre;
        saldo = dinero;
    }
    public void ingresar(double dinero){
        saldo = saldo + dinero;
    }
    public void retirar(double dinero){
        if(dinero<=saldo)
            saldo = saldo-dinero;
    }
    public double checar(double dinero){
        return saldo;
    }
}

to use them within main() you can define the variables as static, in this way call the methods without problem within main() :

 public class Banco {

   private static Cuenta cuenta1 = new Cuenta("Carlos Carrete", 2500);
   private static Cuenta cuenta2 = new Cuenta("Daniela Pabon", 200);

   public static void main(String[] Args) {

         cuenta1.ingresar(200);
         cuenta2.ingresar(100);
         cuenta1.retirar(100);

    }


}
    
answered by 27.08.2017 в 05:50