Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #5105
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | Lew <noone@lewscanon.com> |
| Newsgroups | comp.lang.java.gui |
| Subject | Re: How to initialise a final static String array |
| Date | Sun, 18 Mar 2012 10:28:13 -0700 |
| Organization | albasani.net |
| Lines | 70 |
| Message-ID | <jk55vc$2f4$1@news.albasani.net> (permalink) |
| References | <874ntmxe92.fsf@Compaq.site> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.albasani.net BdCFTbE4cX7O8hNgTxgj6RKzkOjwbDBpy/qb3acvpeoQdAsOTHNvefKbCwfPFrEfmGPszgACFZLBNeWelBhFQ4iykr5jdT8oekDRoPBzSirbo+PdUwEJ7CMspCst/AAd |
| NNTP-Posting-Date | Sun, 18 Mar 2012 17:28:12 +0000 (UTC) |
| Injection-Info | news.albasani.net; logging-data="H1VpApUCK2h13dlC0cRlnQDGzp0opri3KKnPK890o3M1dQLRroFwUj7rgKOJYe+DJ59jXLZ2PFEWo3+RFdq9/63zt0rJNb5k5p4Sa7eN6u1aNauG6eB3W8kmY2ep0eNi"; mail-complaints-to="abuse@albasani.net" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 |
| In-Reply-To | <874ntmxe92.fsf@Compaq.site> |
| Cancel-Lock | sha1:FRMBPE0l+YIQe+2+RqdkPsjXCuc= |
| Xref | csiph.com comp.lang.java.gui:5105 |
Show key headers only | View raw
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.
> 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?
First tell us what you did that didn't work.
http://sscce.org/
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar
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