Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 437 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Updated value of variable from another class.

Tried to find this on many forums but can't find the solution or reason for the solution. I'm trying to get the updated value of a variable from other class by it's reference. The class containing the variable gets the updated value but the other class which creates a reference of the previous class still gets the old value. Here is a sample code:
class A {
private boolean state;
boolean getState() {
return state;
}
}
In the above class, the value of boolean variable state, keeps on changing by an ActionEvent.
class B {
void go() {
A a = new A();
while(true) {
if(a.getState()) {
//CODE
}
}
}
}
Here, a.getState(), never returns true, even when class A has contains updated state. Please suggest.
Here are some solutions that I found but can't find the reason::
1:
class B {
void go() {
A a = new A();
while(true) {
System.out.println(a.getState()); // strange, but works
if(a.getState()) {
//CODE
}
}
}
}
2:
class B {
void go() {
A a = new A();
A aa;
while(true) {
aa = a; // again, works!
if(a.getState()) {
//CODE
}
}
}
}
Please suggest !!!
Best Answer
-
I'll refer you to the canonical article on this: Java is Pass-by-Value, Dammit! - Scott Stanchfield
Answers
-
I'll refer you to the canonical article on this: Java is Pass-by-Value, Dammit! - Scott Stanchfield
-
class A { private boolean state; boolean getState() { return state; }}In the above class, the value of boolean variable state, keeps on changing by an ActionEvent.
There is NOTHING in what you posted that shows 'state' ever gets set to ANY value at all.
And since it is 'private' with no 'setState' method it won't get changed.
-
Use the following class definitions.
class A {
public boolean state;
public boolean getState() {
return state;
}
}
class B {
public void go() {
A a = new A();
if(a.getState()) {
//CODE
}
}
}
-
Without a more complete listing of your code (or a more clear explanation of what you are trying to achieve), it is hard to help you. But in your code, as written, there is no way a.state (or aa.state) can get modified - they are local variables, they don't escape the scope of the method go(), and so they cannot possibly be mutated by another thread.
Try to write a program that more accurately captures what you are trying to do, and maybe we can help you further?
-
I'd rather keep the attribute private, but change the constructor method to set a default value to the attribute (whichever makes more sense) + add a new constructor that would allow to get the value for the state attribute.
-
add a new constructor that would allow to get the value for the state attribute.
I think you mean add a new 'method'?
Besides - this thread is 5 months old and appears to have been abandoned by OP.