0

I am new to java so help is much appreciated.

I have a few classes. I want to pass the user input from my GUI class to my Basket class where it will update the value stored in the variable. Below is what i currently have but it doesn't seem to be updating the variable.

Class One - GUI Class: I would like to pass the 'seats' to the basket class

Basket b;
b = new Basket();   
String seats = JOptionPane.showInputDialog("Enter the number of seats to book");
try {
    int currentValue = Integer.parseInt(seats);
    int newValue = currentValue;    
    b.setSeatsBooked(newValue);
    //some code emitted
}

Second Class - Basket Class, I would like the 'seats' to be passed into this class and stored in the instance variable.

public class Basket {
    private int seatsBooked;
    public int getSeatsBooked() {
        return seatsBooked;
    }
    public void setSeatsBooked(int seatsBooked) {
        this.seatsBooked = seatsBooked;
    }
}

This is in another class where i see the result as 0:

Basket b;
b = new Basket();
lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());
3
  • 1
    are you sure the value is not getting updated? Commented Feb 23, 2017 at 19:46
  • what are you getting as output for the input you are using....???? Commented Feb 23, 2017 at 19:48
  • @WasiAhmad Please see my updated code as im trying to see that variable in another class where it is showing 0. Commented Feb 23, 2017 at 19:51

3 Answers 3

3

This is the reason of the wrongly behaviour:

Basket b;
b = new Basket();

lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());

you are creating a new object! so the default value is zero for that

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

3 Comments

Yes i just realised. So how would i fix this?
for the third class you need to pass that value as parameter,,,
You could also pass the reference variable 'b' to the third class, especially if you're going to use it's properties/methods for anything else there.
2

You need to pass the class object for which you have stored the value. If you create a new instance of a class, how can you expect that object will contain that value?

In the following two cases,

Basket b;
b = new Basket();   
String seats = JOptionPane.showInputDialog("Enter the number of seats to book");
try {
    int currentValue = Integer.parseInt(seats);
    int newValue = currentValue;    
    b.setSeatsBooked(newValue);
    //some code emitted
}

and

Basket b;
b = new Basket();
lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());

Are the object b of class Basket same in both blocks? Answer is No. So, you are getting zero value when you call b.getSeatsBooked() from the newly created object b of class Basket in the second block of code.

There are two way to solve this problem.

  1. Best solution is to pass the class object (as parameter) to the third class where you are calling the getSeatsBooked() method.

Example: Suppose your another class is X and there is a function called method(). Then you can pass Basket class object as parameter to that function where you can access it to get the updated value. The code may look like as follows.

class X{
    // other code
    public void method(Basket b){
        lblMovieSelection = new JLabel("You have booked:" + b.getSeatsBooked());
    }
}
  1. If you want share the variable seatsBooked across all class instances, you can declare the variable as static. (I guess this is not what you are looking for)

1 Comment

Yeah ok i think first solution is better. But im stuck on how to pass the variable to my third class. Do you have any example of how it works because i cant find any on internet.
-1

Try making the variable static, an example for this would be:

public static int testInt = 0;

The way you would call this in the other class is up to you, there are 2 options that you can use.

//NOT CLASS NAME IS FOR YOUR ACTUAL CLASS NAME.
ClassName varName = new ClassName();
varname.testInt;

//OR YOU CAN USE
ClassName.testInt;

4 Comments

variables don't have to be static in order to be passed to another object
Thats the part that you are going to downvote me on..?
He's using a public setter and passing the captured integer to that. This answer is completely wrong.
You can avoid using public setters its up to him I explained that.

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.