0

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;
        }
                
}
4
  • Theoretically you can't access a local variable(in this case a) from outside its scope. In your example the object a has method scope and won't be accessible from outside customerDetails() function. Commented Sep 13, 2020 at 3:17
  • You should return that Customer object you made in your static method, and then you have a valid factory pattern. Also, encapsulate your fields (make them private, look up getters and setters) Commented Sep 13, 2020 at 3:25
  • What you can do is change the return type of customerDetails() to Customer instead of void, and return the object a. And try accessing it from within your BookingConfirmation class. Commented Sep 13, 2020 at 3:40
  • how do i return object a and access it from BookingConfirmation class? Commented Sep 13, 2020 at 3:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I just want to copy the 3 variables from Customer class and print them in sentences (using system.out.println() all done in terminal) in BookingConfirmation class. Scanner gets name in Customer class. BookingConformation class prints. "We have received your hotel booking (name from Customer class - variable customerName).
0

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 (cleared from memory,Garbage collected), 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();

11 Comments

I thought I did create getters for all 3? Do I need to create the getters in the BookingConfirmation class?
You did create getters. My answer is simply going over both so that it is more informative for the general public. Now, what does your print statement prints?
The System.out.println("Thanks for your booking " + a.getCustomerName()); code is located in the BookingConfirmation but to get the customerName from Customer should I use Customer.getCustomerName() instead? Would you mind typing the code I would need to use in BookingConfirmation class to access the getter from Customer class for customerName?
If you use Customer.getCustomerName(), that's a static method. You don't have a static method. A static method is one which is called from the class itself, rather than from an instantiated object of the class. I see what your error is. You are creating the new customer object within your customerDetails method, and this method does not return anything. The customer object you're creating is being destroyed right after that method is over, because you're not holding any reference to it. You need to change that method to return a Customer
I would also like to ask: are you learning programming by yourself or as part of a university course? If it's the latter, I would seriously consider grabbing your text book and revisiting the first chapters. I'm not saying this to discourage you, but rather, to stop you from making the mistakes I made when I learned. Your knowledge at the moment is a block of Swiss cheese. It might look like it's solid but it's full of holes. Methods/functions and classes are at the heart of object oriented programming. You should dominate this before going any further or you will develop bad practices.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.