How to deference a String?

N

Nick Coleman

I guess I'm still stuck in C programming mode, despite two semesters of
Java.

I have a bunch of static finals, one of which is S1 = "Database";

I am passed a String containing the value "S1". I want to use the value
of the String to refer to the constants and resolve (in this case) to
"Database". So that:

String static final S1 = "Database";
String key = "S1";
String result = (insert tricky stuff here); // result is "Database"

I can't use a switch statement as there is no consistency in the format
of the static constants.

I can't use a TreeMap to store the values of the constants as keys along
with their associated values (spec prevents it).

Is there a way to resolve "S1" to S1?

thanks for any help,
Nick
 
H

hilz

String static final S1 = "Database";
String key = "S1"; .....
Is there a way to resolve "S1" to S1?


I am not sure i understand what you're trying to achieve here, but to get a
reference to the filed S1, given that you know its name, you need to use
"reflection", similar to the following:

import java.lang.reflect.*;


Field aField = this.getClass().getField("S1");
String tempString = (String)aField.get( this);



but if i really understand what you're trying to do, i doubt you need to use
reflection.
thanks, and good luck
hilz
 
S

Sudsy

Nick said:
String static final S1 = "Database";
String key = "S1";
String result = (insert tricky stuff here); // result is "Database"

First off, that won't compile: you need to specify the modifiers before the
type, e.g.:

static final String S1 = "Database";

As for the tricky stuff, try this:

import java.lang.reflect.Field;

public class X {
static final String S1 = "Database";

public static void main( String args[] ) {
String key = "S1";
String value = null;
try {
value = (String) X.class.getDeclaredField( key
).get( null );
}
catch( NoSuchFieldException e ) {
// if named field doesn't exist
}
catch( IllegalAccessException e ) {
// shouldn't happen with a class static field
}
catch( ClassCastException e ) {
// if named field is not of type String
}
System.out.println( value );
}
}

ps. I have no idea why you would want to do this...
 
M

Mike Schilling

Nick Coleman said:
I guess I'm still stuck in C programming mode, despite two semesters of
Java.

I have a bunch of static finals, one of which is S1 = "Database";

I am passed a String containing the value "S1". I want to use the value
of the String to refer to the constants and resolve (in this case) to
"Database". So that:

String static final S1 = "Database";
String key = "S1";
String result = (insert tricky stuff here); // result is "Database"

I can't use a switch statement as there is no consistency in the format
of the static constants.

You can't switch on strings at all.
I can't use a TreeMap to store the values of the constants as keys along
with their associated values (spec prevents it).

What exactly does the spec say that prevents it? That seems like an
arbitrary restriction, the kind that would appear only in ...

A school assignment. I see.
 
S

Stefan Schulz

I can't use a TreeMap to store the values of the constants as keys along
with their associated values (spec prevents it).

Use a HashMap, then ;)

If you are gunning at Reflection, see the other answers. If not, i would
like to know
more why using some Map implementation (which would be the logical thing
to do) is
not possible.
 
X

xarax

Nick Coleman said:
I guess I'm still stuck in C programming mode, despite two semesters of
Java.

I have a bunch of static finals, one of which is S1 = "Database";

I am passed a String containing the value "S1". I want to use the value
of the String to refer to the constants and resolve (in this case) to
"Database". So that:

String static final S1 = "Database";
String key = "S1";
String result = (insert tricky stuff here); // result is "Database"

I can't use a switch statement as there is no consistency in the format
of the static constants.

I can't use a TreeMap to store the values of the constants as keys along
with their associated values (spec prevents it).

Is there a way to resolve "S1" to S1?

thanks for any help,
Nick

HashMap translate = new HashMap(); // Or HashTable if multi-threading
String static final S1 = "Database";
String key = "S1";

// all keys must be immutable and naturally ordered
translate.add(key,S1); // add all {key,value} pairs

String result = (String) translate.get(key); // result is "Database"

This is how keys are resolved to values in Java.
Throw away the spec.
 
N

Nick Coleman

Mike Schilling wrote:

What exactly does the spec say that prevents it? That seems like an
arbitrary restriction, the kind that would appear only in ...

A school assignment. I see.

Well, it's not school. Yes, I am doing an assignment with unrealistic
specs, but this particular problem is my own creativity at work.

I don't want to use Maps or Sets because I'd have to hard code magic
numbers. That's against the rules, yes?

Thanks to everyone who replied; reflect was what I was looking for.
 
M

Mike Schilling

Nick Coleman said:
Mike Schilling wrote:



Well, it's not school. Yes, I am doing an assignment with unrealistic
specs, but this particular problem is my own creativity at work.

I don't want to use Maps or Sets because I'd have to hard code magic
numbers. That's against the rules, yes?

How does

public static final String KEY_01 = "value1";
public static final String KEY_02 = "value2";
...

Map pairs = new HashMap();
pairs.put("KEY_01", KEY_01);
pairs.put("KEY_02", KEY_02);
...

hardcode magic numbers?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top