Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13039

Re: How to initialise a final static String array

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: How to initialise a final static String array
Date 2012-03-18 10:31 -0700
Organization albasani.net
Message-ID <jk564u$2f4$2@news.albasani.net> (permalink)
References <87vcm2vzje.fsf@Compaq.site> <4f660b33$0$288$14726298@news.sunsite.dk>

Show all headers | View raw


On 03/18/2012 09:20 AM, Arne Vajhøj wrote:
> On 3/18/2012 8:55 AM, Cecil Westerhof wrote:
>> I want to use final for my static variables. I now do for example:
>> private static final Connection conn;
>> private static final Combo container;
>>
>> and
>> static {
>> Connection tempConn;
>> Statement tempStmt;
>> try {
>> Class.forName("org.h2.Driver");
>> tempConn = DriverManager.getConnection(
>> "jdbc:h2:tcp://localhost/~/databases/stock", "sa", "");
>> tempStmt = tempConn.createStatement();
>> } catch (Exception e) {
>> tempConn = null;
>> tempStmt = null;
>> }
>> conn = tempConn;
>> stmt = tempStmt;
>>
>> and
>> if (conn == null) {
>> throw new Exception("Could not initialise");
>> }
>>
>> The only problem I have is with my array of String.
>> I now have:
>> private static final String[] titles = {
>> "Stock",
>> "Number",
>> "Dare",
>> "Container"
>> };
>>
>> Because when I put it in the static block I get an error.
>> Beside this I can execute a statement like:
>> titles[0] = "changed";
>>
>> Can I get the initialisation in the static part? And in such a way
>> that the elements can not be changed?
>
> Other have already explained what you can not and what you can.
>
> I will just add that you should carefully evaluate whether
> you really want a class with a static field with a
> database connection. In almost all cases it is a disaster
> waiting to happen.

As I stated in an answer to the other multipost of this message:

Cecil Westerhof wrote:
 > I want to use final for my static variables. I now do for example:
 >     private static final Connection conn;

DO NOT USE TAB CHARACTERS TO INDENT USENET POSTS.

Use spaces, up to four per indent level.

 >     private static final Combo      container;
 >
 > and
 >     static {
 >         Connection tempConn;
 >         Statement  tempStmt;
 >         try {
 >             Class.forName("org.h2.Driver");
 >             tempConn = DriverManager.getConnection(
 >                     "jdbc:h2:tcp://localhost/~/databases/stock", "sa", "");
 >             tempStmt = tempConn.createStatement();
 >         } catch (Exception e) {
 >             tempConn = null;
 >             tempStmt = null;
 >         }
 >         conn = tempConn;
 >         stmt = tempStmt;

Static JDBC connections, and especially static JDBC statements, are a very bad 
idea.

Very bad.

Statements should be local in scope, not even instance level. (Usually.) Even 
connections should only be instance members if you have rather short-lived 
objects holding them.

 > and
 >         if (conn == null) {
 >             throw new Exception("Could not initialise");

What kind of exception /should/ you throw? Not 'Exception'.

 >         }
 >
 > The only problem I have is with my array of String.
 > I now have:
 >     private static final String[] titles = {
 >         "Stock",
 >         "Number",
 >         "Dare",
 >         "Container"
 >     };
 >
 > Because when I put it in the static block I get an error.

That doesn't make sense. It's a perfectly legitimate initialization.

http://sscce.org/
please.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to initialise a final static String array Cecil Westerhof <Cecil@decebal.nl> - 2012-03-18 13:55 +0100
  Re: How to initialise a final static String array Jeff Higgins <jeff@invalid.invalid> - 2012-03-18 09:25 -0400
  Re: How to initialise a final static String array Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-18 09:24 -0400
  Re: How to initialise a final static String array Arne Vajhøj <arne@vajhoej.dk> - 2012-03-18 12:20 -0400
    Re: How to initialise a final static String array Lew <noone@lewscanon.com> - 2012-03-18 10:31 -0700
      Re: How to initialise a final static String array Arne Vajhøj <arne@vajhoej.dk> - 2012-03-18 19:04 -0400
        Re: How to initialise a final static String array Lew <noone@lewscanon.com> - 2012-03-18 22:16 -0700
          Re: How to initialise a final static String array Arne Vajhøj <arne@vajhoej.dk> - 2012-03-20 20:01 -0400

csiph-web