can java.util.properties be static values??

M

Mike G.

I'm still learning java....after 5 years.....so bear with me.


Is is possible to read values from a properties file but have those
variables be static?

If so, how?

I though you can only declare static


Thanks,

-Mike
 
A

Alan Meyer

Mike G. said:
Is is possible to read values from a properties file but have those
variables be static?
Sure.

If so, how?

If you have static variables, you can assign to them
like any other variables. You can assign values taken
from a properties file, or you can create a static
Properties object and assign to it.

The only problem is that you can't do it in the declaration
of the static variables themselves. There's no way to
execute the logic in a static declaration.
I thought you can only declare static

Right. So all you have to do is to initialize the
static in your main routine - before you make any
other use of the classes, instead of initializing it
in the declaration itself.

Alan
 
C

Christophe Vanfleteren

Alan said:
The only problem is that you can't do it in the declaration
of the static variables themselves. There's no way to
execute the logic in a static declaration.

I might be misunderstanding you, but the static block allows you to "execute
logic".

Consider the following:


public class A {


static String prop;

static {
//load properties from file
//this will get executed when the class gets loaded
Properties p = ...
prop = p.getProperty("prop");

}
}
 
M

Mike G.

Thanks for your responses.

Actually, I didn't mean "static". I meant "final"...i'm an idiot sometimes!

I guess it doesn't really matter because I am the one doing the programing
for my project, I guess I was looking for a little protection in case I
tried to modify one of the property values.

-Mike
 
V

Virgil Green

Mike G. said:
Thanks for your responses.

Actually, I didn't mean "static". I meant "final"...i'm an idiot sometimes!

I guess it doesn't really matter because I am the one doing the programing
for my project, I guess I was looking for a little protection in case I
tried to modify one of the property values.

Honestly, as I read your question I kept thinking "I wonder if he means
final".

The answer, still, is yes. Final means that once a value is assigned, it
cannot be changed. More than once, I've used static final variables that use
static block initializers to establish the value for the variable.

public class StaticFinal {

final static String myValue;

static {
myValue="myValue";
}

public static void main(String[] args) {
System.out.println(myValue);
}
}

- Virgil
 
A

Alan Meyer

Christophe Vanfleteren said:
I might be misunderstanding you, but the static block allows you to "execute
logic".

You didn't misunderstand me. I had a (hopefully temporary)
brain malfunction. You're right of course. A static block is the
answer - even if it is to the question I answered instead of the
question Mike G. meant to ask.

Sorry for the confusion.

Alan
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top