You are not logged in.

#1 2010-07-07 05:17:10

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Java: String -> variable name?

I'm writing a program where I want to have generic method for changing e.g. int variables within an object. So for instance I would have some other object invoke

fooObject.setIntStat(bar, 3)

and it would set the variable called "bar" to 3.

How, if at all, can I do this?

(And yes, I know, total noob question. neutral )

Offline

#2 2010-07-07 05:31:45

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Java: String -> variable name?

The only way I can think of doing it would be an ugly if/else tree. I don't believe there's a dynamic solution. IMO, stick to the Java paradigm (object oriented orgasm) and write accessors and mutators. It's more verbose, but its easier to grok at a glance and is far more maintainable.

Offline

#3 2010-07-07 05:41:20

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Java: String -> variable name?

falconindy wrote:

The only way I can think of doing it would be an ugly if/else tree. I don't believe there's a dynamic solution. IMO, stick to the Java paradigm (object oriented orgasm) and write accessors and mutators. It's more verbose, but its easier to grok at a glance and is far more maintainable.

Okay thanks. And oyf. That is going to be very verbose indeed.

Offline

#4 2010-07-07 06:15:55

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Java: String -> variable name?

Hmm wait a minute. What about hashmaps? Couldn't I map various strings to variable names using one?

Offline

#5 2010-07-07 06:21:28

PJ
Member
From: Sweden
Registered: 2005-10-11
Posts: 602

Re: Java: String -> variable name?

Or maybe you could use Reflection:
http://stackoverflow.com/questions/2765 … me-in-java

Offline

#6 2010-07-07 11:22:41

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Java: String -> variable name?

Wow, reflection looks fairly cool. Go figure, Java even objectifies fields.

Offline

#7 2010-07-07 12:08:07

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Java: String -> variable name?

Apparently Reflection can also break things hugely, and keep stuff from working in e.g. sandboxed applets. hmm

Offline

#8 2010-07-07 12:30:14

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Java: String -> variable name?

Now Python on the other hand has a vars() method that does *exactly* what I want. Go figure.

Offline

#9 2010-07-07 13:35:07

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Java: String -> variable name?

Gullible Jones wrote:

Hmm wait a minute. What about hashmaps? Couldn't I map various strings to variable names using one?

Better solution:  just keep the variables in a hash to begin with.  It's what they're there for.

class Foo {
    java.util.Map<String, Object> attributes;
    Foo() {
        this.attributes = new java.util.HashMap<String,Object>();
    }
    Object setAttribute(String key, Object value) {
        return attributes.put(key, value);
    }
    Object getAttribute(String key) {
        return attributes.get(key);
    }
    public static void main(String[] args) {
        Foo obj = new Foo();
        obj.setAttribute("bar", 42);
        System.out.printf("bar is %d\n", obj.getAttribute("bar"));
    }
}

Write accessor methods using the hash as a backend if you so desire, and replace Object with Integer if you're only interested in ints.

Now Python on the other hand has a vars() method that does *exactly* what I want. Go figure.

Python has built-ins getattr and setattr, which are more directly related to what you want and can be overridden within a class IIRC.

Offline

#10 2010-07-07 18:16:08

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Java: String -> variable name?

falconindy wrote:

The only way I can think of doing it would be an ugly if/else tree. I don't believe there's a dynamic solution. IMO, stick to the Java paradigm (object oriented orgasm) and write accessors and mutators. It's more verbose, but its easier to grok at a glance and is far more maintainable.

Okay thanks. And oyf. That is going to be very verbose indeed.

Offline

#11 2010-07-07 18:16:52

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Java: String -> variable name?

Hey thanks Trent. It may take me some time to wrap my mind around, but it looks like that will work. tongue

Offline

Board footer

Powered by FluxBB