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


Groups > comp.lang.java.gui > #5104

Re: How to initialise a final static String array

Path csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.gui
Subject Re: How to initialise a final static String array
Date Sun, 18 Mar 2012 09:50:29 -0700
Organization A noiseless patient Spider
Lines 60
Message-ID <jk53ol$ghd$1@dont-email.me> (permalink)
References <874ntmxe92.fsf@Compaq.site>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sun, 18 Mar 2012 16:50:29 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="16941"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QtJLghxKmsMW7/BDAtL0Z"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20120312 Thunderbird/11.0
In-Reply-To <874ntmxe92.fsf@Compaq.site>
Cancel-Lock sha1:TrtcOvPbzXwa6+cFngBZ/DV4G0A=
Xref csiph.com comp.lang.java.gui:5104

Show key headers only | View raw


On 3/18/2012 5:52 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?
>

public class test {
     static final String[] words;

     static {
         words = new String[] {"hello","world"};
     }

     public static void main(String[] args) {
         System.out.println(words[0]);
     }
}

Don't know why you would want to do that though.

-- 

Knute Johnson

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to initialise a final static String array Cecil Westerhof <Cecil@decebal.nl> - 2012-03-18 13:52 +0100
  Re: How to initialise a final static String array Cecil Westerhof <Cecil@decebal.nl> - 2012-03-18 13:54 +0100
  Re: How to initialise a final static String array Knute Johnson <nospam@knutejohnson.com> - 2012-03-18 09:50 -0700
  Re: How to initialise a final static String array Lew <noone@lewscanon.com> - 2012-03-18 10:28 -0700
  Re: How to initialise a final static String array Roedy Green <see_website@mindprod.com.invalid> - 2012-03-18 18:12 -0700
    Re: How to initialise a final static String array Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-03-19 09:34 -0700

csiph-web