Can't call method from another class in Java

Dasphillipbrau :

I'm new to Java and honestly its OOP focus is quite taxing for me at the moment.

For an Uni project where we're meant to practice this focus, I'm tasked with creating at least 2 classes: One class should be for an airline customer and the other class should contain methods to register their purchase.

I have a main file, a Persona (Person) class, and a RegistroCompra (Purchase registration) class. Person is supposed to have all of the following attributes, which I'm handling as private variables so that every instance of Person can get one of their own. (The attributes being asked for are stuff like personal data, ticket number, seat number and such)

public class Persona {



    private String nombrePasajero;
    private String apellidoPasajero;
    private String generoPasajero;
    private String pasaportePasajero;
    private String numTiquetePasajero;
    private String numVueloPasajero;
    private String destinoPasajero;
    private String asientoString;
    private int precioBoleto;
    private int edadPasajero;
    private int numAsientoPasajero;

    //Constructor

    public Persona(String nombre, String apellido, String genero, int edad, String pasaporte) {

        nombrePasajero = nombre;
        apellidoPasajero = apellido;
        generoPasajero = genero;
        pasaportePasajero = pasaporte;
        edadPasajero = edad;

    }

    public void setDestino() {
        destinoPasajero = RegistroCompra.obtenerDestino();
    }

And my RegistroCompra class, which is meant to set the data related not to personal information but to the information of destination, flight number and such. All of the data set in RegistroCompra has to be fetched by Persona, because only Persona will be printed in main to verify all of the information.

public class RegistroCompra {

    private String destino;

    public void seleccionarDestino() {
    Scanner input = new Scanner(System.in);
    System.out.println("Por favor digite el destino, las opciones actuales son Nicaragua o Panama\n");

    String destino = input.nextLine();
}
    public String obtenerDestino() {
        return destino;
    }

}

However, I get an error at the Persona.setDestino() method, saying "non-static method obtenerDestino cannot be referenced from astatic context"

I don't understand why this is going on. If I try to turn RegistroCompra.obtenerDestino() into a static method, I get an error because "destino is a non static variable", but it's being defined as public in the RegistroCompra class...

royalghost :

You have to do something like below:

public class Persona {
   ...
   //You should have instance of RegistroCompra
   RegistroCompra registraCompra = new RegistroCompra();
   public void setDestino() {
        //Option 1: Explicitly call the method
        registraCompra.seleccionarDestino();
        destinoPasajero = registraCompra.obtenerDestino();
    }
}

public class RegistroCompra {

    private String destino;

    public RegistroCompra(){
       //Option 2 : Call the method in constructor
       registraCompra();
    }
    public void seleccionarDestino() {
    ...
    //Set the input to the class level variable destino
    this.destino = input.nextLine();
}
    public String obtenerDestino() {
        return this.destino;
    }

}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416242&siteId=1