2

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...

Dasphillipbrau
  • 524
  • 2
  • 8
  • 17

3 Answers3

3

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;
    }

}


royalghost
  • 2,773
  • 5
  • 23
  • 29
  • Hey Is it a problem if RegistroCompra has no constructor? At that point, what data does the instance of RegistroCompra contains? And why are you using this. instead of RegistroCompra? I understand it's a reference to the class, but why not use class name? – Dasphillipbrau Feb 12 '20 at 03:38
  • You need to invoke the method seleccionarDestino() either through constructor or explicitly by calling the method in order to take a the input. It's your design decision. – royalghost Feb 12 '20 at 03:41
  • RegistroCompra is the name of the class. this refers to the current instance (object) of the class. this.destino clarifies you mean the member variable. destino = would also work, but not String destino = which is its own new variable. – Jeremy Kahan Feb 12 '20 at 03:44
1

You can do this making destino variable and obtenerDestino() method static. Check the below changes to RegistroCompra class:

public class RegistroCompra {
    private static 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 static String obtenerDestino() {
        return destino;
    }
}
Anuradha
  • 570
  • 6
  • 20
0

You are calling an instance method without having an instance. You have to instantiate the class (create an instance from that class) before you are able to call instance methods.

Jonas Fagundes
  • 1,519
  • 1
  • 11
  • 18
  • Now that I get that part, why is it not letting me turn obtenerDestino into a static class? And would there be any downside to turning RegistroCompra into a static class? Its job is only to set some values and return them so that they can be assigned to the attributes of Persona – Dasphillipbrau Feb 12 '20 at 03:42
  • Because `obtenerDestino` is accessing an instance variable (`destino`). – Jonas Fagundes Feb 12 '20 at 03:44