reflection-accessing dynamically variables from a class.

G

Gunther Hebein

Hello,

i am looking for a solution to the following problem.
I have declared a class that contains several static final int constants.
I want to read their values in relation to some String input from a
database.

So if I get the input "DEBUG" in Class A as String from the database I
want to read from Class B its public static final int DEBUG;
if I get instead of "DEBUG" as String for example "LIB", I want to read
from Class B its public static final int LIB with the same code-fragment.
I know that there is a way to do this, but I donm't know exactly how. I
do not want to solve this by if...then or case...switch.

Is there a way to call a funtion from a class with a String, for example
if input String is "DEBUG" to my Class A, it calls B.getDEBUG()?


Kind regards,
gunther
 
D

Daniel Pitts

Gunther said:
Hello,

i am looking for a solution to the following problem.
I have declared a class that contains several static final int constants.
I want to read their values in relation to some String input from a
database.

So if I get the input "DEBUG" in Class A as String from the database I
want to read from Class B its public static final int DEBUG;
if I get instead of "DEBUG" as String for example "LIB", I want to read
from Class B its public static final int LIB with the same code-fragment.
I know that there is a way to do this, but I donm't know exactly how. I
do not want to solve this by if...then or case...switch.

Is there a way to call a funtion from a class with a String, for example
if input String is "DEBUG" to my Class A, it calls B.getDEBUG()?


Kind regards,
gunther
You can do this, but it is dangerous to do. You actually have the right
subject line; use the java.lang.reflect package.

You might also consider using an enum instead of reflective method names.
 
S

Stefan Ram

Gunther Hebein said:
I have declared a class that contains several static final int constants.
I want to read their values in relation to some String input from a
database.

You also have posted the same question in English language
into the newsgroup »de.comp.lang.java«, but with another
subject.

You should have used German language there, because »de.«
means that this is a German-language newsgroup, and you might
have been able to find a wording in German language that is
more comprehendible.
So if I get the input "DEBUG" in Class A as String from the database I
want to read from Class B its public static final int DEBUG;

Static final int constants are set not later than compile
time. You can not set them at run time.
 
J

Joshua Cranmer

Gunther said:
Hello,

i am looking for a solution to the following problem.
I have declared a class that contains several static final int constants.
I want to read their values in relation to some String input from a
database.

There are several ways to do this.

1. Use enums. It sounds like you want essentially static constants,
which is what enums excel it. <enum name>.valueOf(string) is the way to
go here.

2. If you have something more complex than a static final constant, then
enums will probably not work well (they can still work, mind you, if you
contort them enough). At this point, a static Map<String, ?> works (fill
`?' with the appropriate type).

Both methods do assume that you can modify the definition of class B, or
at least keep a separate copy in reasonable sync. If you cannot, you
will have to resort to reflection. In general, avoid reflection if you
can avoid it at all.

3. Reflection. This is generally an overpowered tool for most tasks.
Warnings aside, this is how you you would use it:
|B.class.getField(string).getInt(null);|, wrapped in a few exception
handlers.

My personal preference is for way #1.
 
D

Daniele Futtorovic

Static final int constants are set not later than compile
time.

Only if the expression assigned to them is a compile-time constant.
Otherwise at runtime.
 
G

GArlington

Only if the expression assigned to them is a compile-time constant.
Otherwise at runtime.

AFAIR "static final" combination means that compilation will fail if
you have NOT initialised the var AND it will NOT allow you to assign a
value to it ANYWHERE in the code => the value "can be not later than
compile time" is absolutely correct IMHO.
 
T

Tom Anderson

AFAIR "static final" combination means that compilation will fail if you
have NOT initialised the var AND it will NOT allow you to assign a value
to it ANYWHERE in the code
Wrong!

=> the value "can be not later than compile time" is absolutely correct
IMHO.

Static blocks.

This:

class Foo{
public static final int X ;
static {
X = Integer.parseInt(System.getProperty("X_VALUE")) ;
}
}

Is entirely legal.

tom
 
T

Tom McGlynn

AFAIR "static final" combination means that compilation will fail if
you have NOT initialised the var AND it will NOT allow you to assign a
value to it ANYWHERE in the code => the value "can be not later than
compile time" is absolutely correct IMHO.

Hmmm....

public class Test {
static final long start = new java.util.Date().getTime();
public static void main(String[] args) {
System.out.println("Start is:"+start);
}
}

gives me a different number every time it runs. Seems pretty clear
that it's being set at run time.

Regards,
Tom McGlynn

seems to compile just fine.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top