intialising static finals with exceptions

Discussion in 'Java' started by Roedy Green, Jan 17, 2008.

  1. Roedy Green

    Roedy Green Guest

    Is there a way to initialise a static final constant with an
    expression that might trigger an exception?

    I played with various ways but Javac thought it might not get
    initialised, or it might get initialised twice or I was ignoring the
    exception.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 17, 2008
    #1
    1. Advertising

  2. Roedy Green

    GArlington Guest

    On Jan 17, 1:14 pm, Roedy Green <>
    wrote:
    > Is there a way to initialise a static final constant with an
    > expression that might trigger an exception?
    >
    > I played with various ways but Javac thought it might not get
    > initialised, or it might get initialised twice or I was ignoring the
    > exception.
    > --
    > Roedy Green, Canadian Mind Products
    > The Java Glossary,http://mindprod.com


    Init it in the constructor inside try ... catch block?
     
    GArlington, Jan 17, 2008
    #2
    1. Advertising

  3. Roedy Green wrote:
    > Is there a way to initialise a static final constant with an
    > expression that might trigger an exception?
    >
    > I played with various ways but Javac thought it might not get
    > initialised, or it might get initialised twice or I was ignoring the
    > exception.


    class MyClass {
    static final int FIXED;
    static final int FIXEDdefaultvalue = 0;
    static { // static constructor block - this goes into <clinit>
    int tmp = FIXEDdefaultvalue;
    try {
    tmp = ForeignObject.mayThrow();
    } catch (Exception ex) {
    // ignore?
    }
    FIXED = tmp;
    }
    }

    class ForeignObject {
    static int mayThrow() throws Exception
    {
    return 1;
    }
    }
     
    Thomas Schodt, Jan 17, 2008
    #3
  4. Roedy Green

    Lord Zoltar Guest

    Roedy Green wrote:
    > Is there a way to initialise a static final constant with an
    > expression that might trigger an exception?
    >
    > I played with various ways but Javac thought it might not get
    > initialised, or it might get initialised twice or I was ignoring the
    > exception.
    > --
    > Roedy Green, Canadian Mind Products
    > The Java Glossary, http://mindprod.com


    Have you tried using a static initializer for the class?
     
    Lord Zoltar, Jan 17, 2008
    #4
  5. Roedy Green

    Lew Guest

    Roedy Green wrote:
    >> Is there a way to initialise a static final constant with an
    >> expression that might trigger an exception?
    >>
    >> I played with various ways but Javac thought it might not get
    >> initialised, or it might get initialised twice or I was ignoring the
    >> exception.


    GArlington wrote:
    > Init it in the constructor inside try ... catch block?


    It's a *static final* field. Constructor initialization would fail miserably.

    Initialize it in a static initialization block, and make sure that something
    reasonable happens in the catch{} block.

    --
    Lew
     
    Lew, Jan 17, 2008
    #5
  6. Roedy Green

    Roedy Green Guest

    On Thu, 17 Jan 2008 09:18:24 -0500, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >Initialize it in a static initialization block, and make sure that something
    >reasonable happens in the catch{} block.


    see http://mindprod.com/jgloss/initialisation.html#CATCH22 for a
    concrete example.

    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 17, 2008
    #6
  7. Roedy Green

    Roedy Green Guest

    On Thu, 17 Jan 2008 06:10:09 -0800 (PST), Lord Zoltar
    <> wrote, quoted or indirectly quoted someone who
    said :

    >Have you tried using a static initializer for the class?

    Without success. Looks like Thomas has the solution. Thanks.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 17, 2008
    #7
  8. Roedy Green

    Roedy Green Guest

    Roedy Green, Jan 17, 2008
    #8
  9. Roedy Green

    Lew Guest

    Roedy Green wrote:
    > On Thu, 17 Jan 2008 09:18:24 -0500, Lew <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >> Initialize it in a static initialization block, and make sure that something
    >> reasonable happens in the catch{} block.

    >
    > see http://mindprod.com/jgloss/initialisation.html#CATCH22 for a
    > concrete example.


    Another way around it is with a static initialize() method:

    private static final VAR = initializeVar();

    The mindprod example has a couple of problems that obscure its pedagogical
    purpose:

    > try
    > {
    > static final URL WEBSITEURL = new URL( "http://mindprod.com" );
    > }
    > catch ( MalformedURLException e )
    > {
    > WEBSITEURL = null;
    > // will not compile with or without the above line.
    > }


    This has syntax errors other than the possible duplicate assignment. Here's a
    cleaned-up version that exhibits the catch-22:

    static final URL WEBSITEURL;
    static
    {
    try
    {
    WEBSITEURL = new URL( "http://mindprod.com" );
    }
    catch ( MalformedURLException e )
    {
    WEBSITEURL = null;
    // will not compile with or without the above line.
    }
    }
    > ~/projects/testit/src/testit/StatInitter.java:23: variable WEBSITEURL might already have been assigned
    > WEBSITEURL = null;
    > 1 error


    The proposed fix:
    > static final URL WEBSITEURL;
    >
    > static
    > {
    >
    > try
    > {
    > URL temp = new URL( "http://mindprod.com" );
    > }
    > catch ( MalformedURLException e )
    > {
    > temp = null;
    > // Will not complain because temp is not a static final.
    > }
    >
    > WEBSITEURL = temp;
    > }


    has a syntax error - the variable 'temp' goes out of scope at the end of the
    try block, making it inaccessible to the catch block or the final assignment.
    It should be scoped to the entire static block:

    static final URL WEBSITEURL;
    static
    {
    URL temp;
    try
    {
    temp = new URL( "http://mindprod.com" );
    }
    catch ( MalformedURLException e )
    {
    temp = null;
    // Will not complain because temp is not a static final.
    }
    WEBSITEURL = temp;
    }

    This static block would be embedded into the private static method suggested
    above, if that route were taken.

    --
    Lew
     
    Lew, Jan 17, 2008
    #9
  10. Roedy Green

    Roedy Green Guest

    On Thu, 17 Jan 2008 14:50:24 -0500, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >This has syntax errors other than the possible duplicate assignment. Here's a
    >cleaned-up version that exhibits the catch-22:


    How embarrassing. That's what happens when I "clean up" real code to
    make a teaching example without actually compiling it.

    Thanks. Now fixed.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 17, 2008
    #10
  11. Hi!

    Thomas Schodt wrote:

    > class MyClass {
    > static final int FIXED;
    > static final int FIXEDdefaultvalue = 0;
    > static { // static constructor block - this goes into <clinit>
    > int tmp = FIXEDdefaultvalue;
    > try {
    > tmp = ForeignObject.mayThrow();
    > } catch (Exception ex) {
    > // ignore?
    > }
    > FIXED = tmp;
    > }
    > }
    >
    > class ForeignObject {
    > static int mayThrow() throws Exception
    > {
    > return 1;
    > }
    > }


    or:

    Interface ApplicationConstants
    {
    public static final Connection myFinalConnection
    = Initial.getConnection();

    }

    Class Initial
    {
    public static Connection getConnection()
    {
    Connection returnValue = null;
    try
    {
    returnValue = something();
    }
    catch (SQLException sqlex)
    {
    // some logging
    }
    return returnValue;
    }
    }


    brgds

    --
    Gunter Herrmann
    Orlando, Fl, USA
     
    Gunter Herrmann, Jan 17, 2008
    #11
  12. Roedy Green wrote:
    > On Thu, 17 Jan 2008 13:47:59 +0000, Thomas Schodt
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >> FIXED = tmp;

    >
    > thanks, I have documented your solution at
    > http://mindprod.com/jgloss/initialisation.html#CATCH22


    You might want to move the declaration of
    URL temp;
    outside the try block so it is still in scope when we want to use it
    or did you leave that as an exercise for the reader?
     
    Thomas Schodt, Jan 17, 2008
    #12
  13. Roedy Green

    Lew Guest

    Roedy Green wrote:
    > On Thu, 17 Jan 2008 14:50:24 -0500, Lew <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >> This has syntax errors other than the possible duplicate assignment. Here's a
    >> cleaned-up version that exhibits the catch-22:

    >
    > How embarrassing. That's what happens when I "clean up" real code to
    > make a teaching example without actually compiling it.


    You shouldn't be embarrassed. I am privileged to be able to contribute.

    --
    Lew
     
    Lew, Jan 18, 2008
    #13
  14. Roedy Green

    Lew Guest

    Gunter Herrmann wrote:
    > Hi!
    >
    > Thomas Schodt wrote:
    >
    >> class MyClass {
    >> static final int FIXED;
    >> static final int FIXEDdefaultvalue = 0;
    >> static { // static constructor block - this goes into <clinit>
    >> int tmp = FIXEDdefaultvalue;
    >> try {
    >> tmp = ForeignObject.mayThrow();
    >> } catch (Exception ex) {
    >> // ignore?
    >> }
    >> FIXED = tmp;
    >> }
    >> }
    >>
    >> class ForeignObject {
    >> static int mayThrow() throws Exception
    >> {
    >> return 1;
    >> }
    >> }

    >
    > or:
    >
    > Interface ApplicationConstants


    lower-case "i" for the keyword 'interface'

    > {
    > public static final Connection myFinalConnection
    > = Initial.getConnection();


    It's against the spirit of interfaces to implement things, in what they call
    the Constant Interface Antipattern, and creates interesting risks.

    <http://java.sun.com/docs/books/effective/>
    Item 17.

    Also, if you're initializing using the public static method of class Initial
    anyway, why do you need the interface? Just use the Initial method.

    > }
    >
    > Class Initial


    The keyword 'class' is spelled with all lower-case letters.

    > {
    > public static Connection getConnection()
    > {
    > Connection returnValue = null;
    > try
    > {
    > returnValue = something();
    > }
    > catch (SQLException sqlex)
    > {
    > // some logging
    > }
    > return returnValue;
    > }
    > }


    --
    Lew
     
    Lew, Jan 18, 2008
    #14
  15. Roedy Green

    Roedy Green Guest

    On Thu, 17 Jan 2008 23:34:02 +0000, Thomas Schodt
    <> wrote, quoted or indirectly quoted
    someone who said :

    >You might want to move the declaration of
    > URL temp;
    >outside the try block so it is still in scope when we want to use it
    >or did you leave that as an exercise for the reader?


    arrgh. Do I have it right now? I am running a fever. My brain is a
    fog.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 18, 2008
    #15
  16. On 17.01.2008 14:14, Roedy Green allegedly wrote:
    > Is there a way to initialise a static final constant with an
    > expression that might trigger an exception?
    >
    > I played with various ways but Javac thought it might not get
    > initialised, or it might get initialised twice or I was ignoring the
    > exception.


    Just my (late) two cents, additionally to Thomas Schodt's solution:

    If you do not wish to ignore the Exception which might possibly get
    thrown, you can do so, by using something else than a checked exception.

    For instance, an Error:

    class Test {

    public static final int CONSTANT;

    static {
    try {
    CONSTANT = mayFail();
    }
    catch (Exception ex){
    throw new Error(ex);
    }
    }

    static int mayFail() throws Exception {
    return 0;
    }
    }

    A RuntimeException does it too, of course. IOW, the situation you
    describe only ever applies when checked exceptions are involved.

    DF.
     
    Daniele Futtorovic, Jan 23, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    30,128
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,068
    Alex Martelli
    Nov 12, 2003
  3. Replies:
    3
    Views:
    637
    Sherm Pendley
    Apr 16, 2007
  4. Lie
    Replies:
    3
    Views:
    715
  5. Kesh Kissoon
    Replies:
    3
    Views:
    427
Loading...

Share This Page