Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Initialize constants

Thread view: 
John Smith - 13 Jul 2006 22:22 GMT
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.
Oliver Wong - 13 Jul 2006 22:25 GMT
>I have a Constants class and a Variable class.
>
[quoted text clipped - 45 lines]
> 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
John Smith - 13 Jul 2006 22:37 GMT
> >I have a Constants class and a Variable class.
> >
[quoted text clipped - 51 lines]
>
>     - 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
"news:1152825768.284557.43280@35g2000cwc.googlegroups.com..."
"- Show quoted text -"

links in your reply?
Oliver Wong - 13 Jul 2006 22:49 GMT
>> >I have a Constants class and a Variable class.
>> >
[quoted text clipped - 58 lines]
>
> 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
> "news:1152825768.284557.43280@35g2000cwc.googlegroups.com..."
> "- Show quoted text -"
>
> links in your reply?

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

   - Oliver
Hendrik Maryns - 14 Jul 2006 09:51 GMT
Oliver Wong schreef:
>>> >I have a Constants class and a Variable class.
>>> >
[quoted text clipped - 63 lines]
>    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@35g2000cwc.googlegroups.com..."
[quoted text clipped - 3 lines]
>
>    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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.