I've got a bit of a weird problem. I have some primitives (doubles, integers, booleans) that I need to access via reference. I'm making an automatic config system, and it's designed to work with just one function per parameter, addConfigParameter(key, variable), and anytime the reload function is called it should change variable based on the config file. This is possible if I use a reference type, but I'd like it to work for any code so hard coding configurable classes is a no-no. Anyone know of a way I can do this? I tried using the primitive wrapper classes Double vs double but it didn't seem to help.
#Accessing primitives as references
15 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @reef maple! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Double should have worked what the problem you are facing? any examples?
Wrappers are immutable, so that's why they don't work too
Yea, this is the code I create the parameter with
Double val = 1.0;
AutoConfig conf = new AutoConfig();
conf.addConfigValue("testconf", val);```
Then I set it with
```java
private void setValue(Object obj, String key, YamlMapping map){
if(obj instanceof Double){
obj = map.doubleNumber(key);
}```
which I've confirmed runs before I log the value, and map.doubleNumber(key) returns 42, but when I log val it's stil 1.0
I guess I could just create a class that holds a value and use that so I can get a reference to it but I feel like theres a better way than creating a class for every type of parameter I might use
basically, that's bad approach, since java arguments are passed as values always. Consider refactoring your code in a way, that method returns object, and then you assign it to a neened variable.
creating your own wrapper is the solution. And to avoid creating class for each type, you should use generics
like, ```java
class MyWrapper<T> {
T element;
public void setElement(T el) {
element = el;
}
public T getElement() {
return element;
}
}```
you are welcome
That seems to work how I want, thanks again!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.