Initialize constants

J

John Smith

I have a Constants class and a Variable class.

I need to initialize a constant in the Constants class:
public static final String FILE_NAME;

The Variable class has a public String method that returns the full
pathway for the file name.
public String getVariable( String variableName ) throws Exception;

I have a getPath() method in Constants that calls getVariable in
Variable:
private static String getPath( String variableName ) throws Exception {
try {
Variable v = new Variable();
return v.getVariable( variableName );
}
catch ( Exception e )
{
throw e;
}
}

Now to initialize Constants.FILE_NAME, I need to call getPath() to pick
up the pathway.
If I do:

public static final String FILE_NAME = Constants.getPath( "PATH_NAME"
);

I got an "unhandled exception error".



If I wrap "public static final String FILE_NAME = Constants.getPath(
"PATH_NAME" );"
with a try-catch block as follows:

try {
public static final String FILENAME = Constants.getPath("PATH_NAME");
}
catch (Exception e){
throw e;
}
I got:
Syntax error, insert } to complete block
Syntax error on tokens, constructor ConstructorHeaderName expected
instead.

How do I go about initializing the value of FILE_NAME? I *must* use
Variable::getVariable().
Thanks for your help.
 
O

Oliver Wong

John Smith said:
I have a Constants class and a Variable class.

I need to initialize a constant in the Constants class:
public static final String FILE_NAME;

The Variable class has a public String method that returns the full
pathway for the file name.
public String getVariable( String variableName ) throws Exception;

I have a getPath() method in Constants that calls getVariable in
Variable:
private static String getPath( String variableName ) throws Exception {
try {
Variable v = new Variable();
return v.getVariable( variableName );
}
catch ( Exception e )
{
throw e;
}
}

Now to initialize Constants.FILE_NAME, I need to call getPath() to pick
up the pathway.
If I do:

public static final String FILE_NAME = Constants.getPath( "PATH_NAME"
);

I got an "unhandled exception error".



If I wrap "public static final String FILE_NAME = Constants.getPath(
"PATH_NAME" );"
with a try-catch block as follows:

try {
public static final String FILENAME = Constants.getPath("PATH_NAME");
}
catch (Exception e){
throw e;
}
I got:
Syntax error, insert } to complete block
Syntax error on tokens, constructor ConstructorHeaderName expected
instead.

How do I go about initializing the value of FILE_NAME? I *must* use
Variable::getVariable().
Thanks for your help.

Make it so that private static String getPath( String variableName )
doesn't throw an exception. That means you'll have the handle the exception
in a manner other than throwing it.

- Oliver
 
J

John Smith

Oliver said:
Make it so that private static String getPath( String variableName )
doesn't throw an exception. That means you'll have the handle the exception
in a manner other than throwing it.

- Oliver

That would have been an easy solution; except I was explicitly told the
exceptions must be thrown up the stack until the "main program that
uses Constants".

Any other suggestions?

P.S. I am using google groups. How did you generate the
""- Show quoted text -"

links in your reply?
 
O

Oliver Wong

John Smith said:
That would have been an easy solution; except I was explicitly told the
exceptions must be thrown up the stack until the "main program that
uses Constants".

Any other suggestions?

Hmm... How about wrapping the exception in a runtime exception instead?
E.g. throw new InvocationException(e);
P.S. I am using google groups. How did you generate the
""- Show quoted text -"

links in your reply?

I'm not using Google Groups. My newsreader does it automatically.

- Oliver
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oliver Wong schreef:
Hmm... How about wrapping the exception in a runtime exception
instead? E.g. throw new InvocationException(e);

I’d say this is going from bad to worse.
First of all, a class Constants does not seem a very good idea to me.
Neither does Variable (except if you mean some sort of representation
for a mathematical variable for it).

I’d say: rethink the design, and never ever throw Exception(). I.e.
alter the getVariable() method, then the problem melts away.
I'm not using Google Groups. My newsreader does it automatically.

Uh, no, Google does that automatically.

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEt1r/e+7xMGD3itQRAjFmAJ9ucg0a3OEoyYDizoN/lmIRSFGz4QCfW/5q
YIQ1PD1TId09+Au1PV2uwmc=
=cUPJ
-----END PGP SIGNATURE-----
 

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,221
Messages
2,571,133
Members
47,747
Latest member
swapote

Latest Threads

Top