Re: Initialize constants



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

Oliver Wong schreef:
"John Smith" <wleung7@xxxxxxxxx> wrote in message
news:1152826652.322835.187130@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Oliver Wong wrote:
"John Smith" <wleung7@xxxxxxxxx> wrote in message
news:1152825768.284557.43280@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

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);

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.

P.S. I am using google groups. How did you generate the
"news:1152825768.284557.43280@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
"- Show quoted text -"

links in your reply?

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-----
.



Relevant Pages

  • Re: Initialize constants
    ... >I have a Constants class and a Variable class. ... > public static final String FILE_NAME; ... > public String getVariable(String variableName) throws Exception; ...
    (comp.lang.java.programmer)
  • Re: Initialize constants
    ... The Variable class has a public String method that returns the full ... public String getVariable(String variableName) throws Exception; ... Now to initialize Constants.FILE_NAME, I need to call getPathto pick ...
    (comp.lang.java.programmer)
  • Initialize constants
    ... I have a Constants class and a Variable class. ... The Variable class has a public String method that returns the full ... public String getVariable(String variableName) throws Exception; ...
    (comp.lang.java.programmer)
  • Re: Initialize constants
    ... The Variable class has a public String method that returns the full ... public String getVariable(String variableName) throws Exception; ... Now to initialize Constants.FILE_NAME, I need to call getPathto pick ...
    (comp.lang.java.programmer)
  • Re: type declaration characters
    ... the above code gives the string a definite size. ... I hate dynamic strings. ... All vbasic had to do was make some extra custom type declaration chars ...
    (microsoft.public.excel.misc)