'Method doesn't update variable globally?
So, in the beginning of my java class I define globally:
static long world;.
Then I have several functions using world as an argument. One of theses, called setCell does not work as intended, and I can't figure out why. I have tried error searching with println commands, so now my code looks like this:
public static long setCell(long world, int col, int row, boolean newval){
if(col>= 0 && col<8 && row>=0 && row<8){
int bitPosition = col + 8*row;
long newWorld = PackedLong.set(world, bitPosition, newval);
System.out.println(newWorld);
world = newWorld;
System.out.println(world);
return world;
}
else{
return world;
}
}
The main idea of the code is, that it should update world by changing one of its bits with the PackedLong.set method (which is working well) and then return the new updated world.
If we now run:
world =0x20A0600000000000L;
System.out.println(world);
setCell(world, 1, 1, true);
System.out.println(world);
in the main method, we get the following output:
2350984558603665408
2350984558603665920
2350984558603665920
2350984558603665408
From this I have concluded that the commands inside the setCell method works as intended, but that the method does not change world "globally" through the whole code. How can I solve this issue?
Many Thanks! :)
Solution 1:[1]
You have the parameter world
public static long setCell(long world, int col, int row, boolean newval)
This will hide the global variable and instead update the parameter. You should avoid names the hide other variables that are present in the same scope. Instead choose a different name for the parameter.
Solution 2:[2]
First of all, it is bad to use global variables, because they are a hidden dependency. It will be unclear if a class is using world, unless you actually go and find it being used in the code. Code like that can be hard to debug.
long does not have this method to set some bit on it, instead of passing it to some static function, and it updating some global variable (a la C), you would be better off encapsulation it in a class that offers this functionality:
class World {
private long world;
public World(long value) {
this.world = value;
}
public void setCell(int col, int row, boolean newval) {
if(col>= 0 && col<8 && row>=0 && row<8){
int bitPosition = col + 8*row;
world = PackedLong.set(world, bitPosition, newval);
}
}
@Override
public String toString() {
return String.valueOf(world); // Or print a hexadecimal presentation.
}
}
Classes allow you to make plain data (like long) smart. It also allows you to remove a parameter (since it's kept internally).
It will also be much clearer that you're changing world:
World world = new World(0x20A0600000000000L);
System.out.println(world);
world.setCell(1, 1, true); // Aha, 'world' is being altered
System.out.println(world);
Kelvin has the right answer for why your code doesn't work.
Solution 3:[3]
I believe @Jorn Vernee's answer is the best one. https://stackoverflow.com/a/39561744/4506528
However I'll answer your question with an extremely bad coding practice demonstrating how you can achieve this with the language features. If your class is well made, you should never have to do this.
If your class is called Story and a static field called world, you can directly reference it from most places using Story.world. This includes re-assigning it.
public class Story {
public static String world = "Saturn";
public static void main(String[] args) {
doStuff("Earth");
System.out.println(world);
}
public static void doStuff(String world) {
Story.world = "Europa";
world = "Mars";
}
}
Even though this provides an answer to your question, I do not encourage using static fields this way. Please do not put this answer into practice.
Your
longis meant to behave in a very specific way: Its behavior should be encapsulated into a class.Do not use static fields to describe the state of your system.
Change the parameter name of your method to avoid confusion when reading the code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jorn Vernee |
| Solution 3 | Community |
