Hi all, I've got a certain instance in my program where an integer variable, let's call it 'x', is initialised in one part of a class, however the value is not present when the integer is called in another class.

To explain further, I've got 2 classes - 'class1' and 'class2'. The variable is declared in 'class1' as 'x'. At some point in class1, I give 'x' a value, let's say 5.

Now, when I create an object of 'class1' in 'class2', 'x' is automatically equal to 0. Why is it that after running class1, and then linking to class2, the value of 5 is not carried over onto the 'x' object in class2?

I'm sorry if it's a bit confusing, I'll try to explain differently if it's not understandable. Basically I want to know how one can update values of a variable in one class, and have those values updated in another class when you call it (obviously while initialising it during run time).

Thanks :)

Recommended Answers

All 4 Replies

Hi all, I've got a certain instance in my program where an integer variable, let's call it 'x', is initialised in one part of a class, however the value is not present when the integer is called in another class.

To explain further, I've got 2 classes - 'class1' and 'class2'. The variable is declared in 'class1' as 'x'. At some point in class1, I give 'x' a value, let's say 5.

Now, when I create an object of 'class1' in 'class2', 'x' is automatically equal to 0. Why is it that after running class1, and then linking to class2, the value of 5 is not carried over onto the 'x' object in class2?

I'm sorry if it's a bit confusing, I'll try to explain differently if it's not understandable. Basically I want to know how one can update values of a variable in one class, and have those values updated in another class when you call it (obviously while initialising it during run time).

Thanks :)

Please note that I've also asked the question here: http://www.java-forums.org/new-java/32865-how-set-variables-value-one-class-while-having-value-updated-another.html#post145709

Is x an instance variable (ie not static)?
If so, every instance of Class1 has its own value for x.
You can do whatever you like with x in one instance of Class1, and this will have no effect on the value of x for any other new instance of Class1 that you create later.
What are you trying to achieve, exactly?

Is x an instance variable (ie not static)?
If so, every instance of Class1 has its own value for x.
You can do whatever you like with x in one instance of Class1, and this will have no effect on the value of x for any other new instance of Class1 that you create later.
What are you trying to achieve, exactly?

I tried changing x to static, and it worked! Thank you very much :)

It would help to know what you are actually trying to acheive here, as this is probably not the right way to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.