简体   繁体   中英

How do I access variables from another class in Java?

I have a class called Customer with customerName, customerEmail and customerAddress as variables set in an object called a. How do I print the variables customerName, customerEmail and customerAddress in another class called BookingConfirmation? I tried:

System.out.println("Thanks for your booking " + a.getCustomerName());

Here is the code for Customer class:

public class Customer {

String customerName;
String customerEmail;
String customerAddress;
    
    public static void customerDetails() throws IOException {
        
        Customer a = new Customer();
    
        Scanner seekName = new Scanner(System.in);  // Create a Scanner object
        System.out.print("Your name: ");
        a.customerName = seekName.nextLine();  // Read user input
            
        Scanner seekEmail = new Scanner(System.in);  // Create a Scanner object
        System.out.print("Your email: ");
        a.customerEmail = seekEmail.nextLine();  // Read user input
            
        Scanner seekAddress = new Scanner(System.in);  // Create a Scanner object
        System.out.print("Your residential address: ");
        a.customerAddress = seekAddress.nextLine();  // Read user input
        

        System.out.println("Thanks for your booking " + a.getCustomerName());
        System.out.println("Eemail: " + a.getCustomerEmail());
        System.out.println("Address: " + a.getCustomerAddress());
        System.out.println();
    }
    
        public String getCustomerName() {
            return customerName;
        }
        
        public String getCustomerEmail() {
            return customerEmail;
        }
                
        public String getCustomerAddress() {
            return customerAddress;
        }
                
}

I'm not sure I know what you're asking, but it seems that you could do the following:

  1. Have the customerDetails() method return a (the created Customer object) as the result of the method.

  2. Call Customer.customerDetails() from BookingConfirmation , and save returned value in a local variable Customer a .

  3. Then you can call System.out.println("Thanks for your booking " + a.getCustomerName()); inside the BookingConfirmation code.

If you simply want to access the value of a property from outside of the class, then you need to create getters for each one of the properties you want to retrieve.

If you want to modify these values from outside of the class, you need to create a setter.

Getters and Setters are simply methods that allow you to access properties of a class outside of it.

If you have the class Customer and want to access and change it's name from outside, you'd create 2 methods,

//declare the property
private String name;

//getter
public String getName(){
   return this.name;
}

//setter public void setName(String newName){
   this.name = newName;
}

//You can then instantiate a Customer object anywhere else and have access to those //properties

Customer cust = new Customer();
cust.setName("Mark")
System.out.println("Oh hi " + cust.getName());
//output "Oh hi Mark"

Read more on getters and setters

Also, best practices tip: instance variables should always be declared as private to help encapsulation. If no access modifier is provided for an instance variable in Java, it defaults to the default modifier, which makes the variable accessible for every class within the same package.

Edit:

Your specific error is that you are creating a new Customer object within your customerDetails method, and you're not returning this Customer object. Therefore, as soon as the method has been executed, the Customer object you created inside is destroyed, because there is no further reference to it.

You either need to

Method 1: Change the return type of your customerDetails method from void to Customer and add a return a statement at the end, then you would simply need to instantiate a Customer object from your booking class, like so

public Customer customerDetails(){               
    Customer a = new Customer();           
    //your logic to set all of the properties          
    return a;        
}

in your booking class

Customer myCust = new Customer();
myCust = myCust.customerDetails();

I would not prefer this method, because as you see, you're just creating an empty object then reassigning to it. You may alternatively add the static keyword after public so that you can call it without having instantiated an object of the class, like so

booking class

Customer myCust = Customer.customerDetails();

Method 2: remove the Customer a = new Customer() from the customerDetails altogether and simply use this.name = sc.nextLine() to set the name of whatever instance is calling this method.

Then on bookings class, instantiate a new Customer object and call the method.

Customer myCust = new Customer();
myCust.customerDetails();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM