You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Dave <ja...@yahoo.com> on 2008/02/20 16:47:43 UTC

OT: a java question - static initialization

class Foo {
   
       private static int;
   
      static {
         a = 100;
      }
   
      Foo() { 
        
      }
   
  }
   
  Class.forName("package.Foo").newInstance();
   
  The static init block of Foo is not called.
   
  I am using Java 1.5 update14.  It is a bug?
   
  Thanks
  Dave
   
   

       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.

RE: a java question - static initialization

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Dave [mailto:javaone9@yahoo.com] 
> Subject: OT: a java question - static initialization
> 
> class Foo {
>        private static int;
>       static {
>          a = 100;
>       }
>       Foo() { 
>       }
>   }
>    
>   Class.forName("package.Foo").newInstance();
>    
>   The static init block of Foo is not called.

Works fine for me under 1.5.0_14 (and other levels).  Since your example
code is chock full of syntactic and semantic errors, you might want to
correct those first and publish actual code.

Here's what ran for me:

package myPackage;
class Foo {
  private static int a;
  static { a = 100; }
  Foo() { }
  public String toString() { return Integer.toString(a); }
}

package myPackage;
public class FooMain {
  static public void main(String[] args) throws Exception {
    System.out.println(Class.forName("myPackage.Foo").newInstance());
  }
}

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: OT: a java question - static initialization

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kenneth,

Kenneth Westelinck wrote:
| Did a quick test on 1.6.0 update 2 and the static block does get called. I
| guess this is a bug.

Er, this code doesn't look like it should compile:

|> class Foo {
|>
|>       private static int;
|>

Isn't there an expected identifier?

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAke8fUQACgkQ9CaO5/Lv0PBEGQCgrUzyijqDv/5+AnDX6H/h9Xsa
KMAAoJC6KSnAsy4g3x4zNu67J48HzPRV
=eBYe
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: OT: a java question - static initialization

Posted by Kenneth Westelinck <ke...@gmail.com>.
Did a quick test on 1.6.0 update 2 and the static block does get called. I
guess this is a bug.

On Feb 20, 2008 4:47 PM, Dave <ja...@yahoo.com> wrote:

> class Foo {
>
>       private static int;
>
>      static {
>         a = 100;
>      }
>
>      Foo() {
>
>      }
>
>  }
>
>  Class.forName("package.Foo").newInstance();
>
>  The static init block of Foo is not called.
>
>  I am using Java 1.5 update14.  It is a bug?
>
>  Thanks
>  Dave
>
>
>
>
> ---------------------------------
> Never miss a thing.   Make Yahoo your homepage.
>

RE: OT: a java question - static initialization

Posted by "Katilie, John" <Jo...@softwareagusa.com>.
Also, When you say not called. Did you trace it through the debugger?
Could there also be some optimization going on here? 

jfk  

-----Original Message-----
From: Jim Cox [mailto:shakahshakah@gmail.com] 
Sent: Wednesday, February 20, 2008 11:05 AM
To: Tomcat Users List
Subject: Re: OT: a java question - static initialization

On Feb 20, 2008 10:47 AM, Dave <ja...@yahoo.com> wrote:
> class Foo {
>
>        private static int;
>
>       static {
>          a = 100;
>       }
>
>       Foo() {
>
>       }
>
>   }
>
>   Class.forName("package.Foo").newInstance();
>
>   The static init block of Foo is not called.
>
>   I am using Java 1.5 update14.  It is a bug?

FWIW, appears to work here under 1.6 (Linux) and 1.4.2 (Solaris):
jc@jc-ubuntu:~/tmp/jstatic$ java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
jc@jc-ubuntu:~/tmp/jstatic$ cat test.java
public class test {
  public static void main(String [] args)
    throws Exception {
    Object o = Class.forName("foo").newInstance() ;
  }
}
jc@jc-ubuntu:~/tmp/jstatic$ cat foo.java
public class foo {
  private static int a ;

  static {
    a = 100 ;
    System.out.println("a set to " + a) ;
  }

  foo() {
  }
}
jc@jc-ubuntu:~/tmp/jstatic$ javac test.java
jc@jc-ubuntu:~/tmp/jstatic$ javac foo.java
jc@jc-ubuntu:~/tmp/jstatic$ java -classpath "." test
a set to 100

And under 1.4.2:
jc@soyuz:~/tmp/jstatic$ java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
jc@soyuz:~/tmp/jstatic$ javac test.java
jc@soyuz:~/tmp/jstatic$ javac foo.java
jc@soyuz:~/tmp/jstatic$ java -classpath "." test
a set to 100

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: OT: a java question - static initialization

Posted by Jim Cox <sh...@gmail.com>.
On Feb 20, 2008 10:47 AM, Dave <ja...@yahoo.com> wrote:
> class Foo {
>
>        private static int;
>
>       static {
>          a = 100;
>       }
>
>       Foo() {
>
>       }
>
>   }
>
>   Class.forName("package.Foo").newInstance();
>
>   The static init block of Foo is not called.
>
>   I am using Java 1.5 update14.  It is a bug?

FWIW, appears to work here under 1.6 (Linux) and 1.4.2 (Solaris):
jc@jc-ubuntu:~/tmp/jstatic$ java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
jc@jc-ubuntu:~/tmp/jstatic$ cat test.java
public class test {
  public static void main(String [] args)
    throws Exception {
    Object o = Class.forName("foo").newInstance() ;
  }
}
jc@jc-ubuntu:~/tmp/jstatic$ cat foo.java
public class foo {
  private static int a ;

  static {
    a = 100 ;
    System.out.println("a set to " + a) ;
  }

  foo() {
  }
}
jc@jc-ubuntu:~/tmp/jstatic$ javac test.java
jc@jc-ubuntu:~/tmp/jstatic$ javac foo.java
jc@jc-ubuntu:~/tmp/jstatic$ java -classpath "." test
a set to 100

And under 1.4.2:
jc@soyuz:~/tmp/jstatic$ java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
jc@soyuz:~/tmp/jstatic$ javac test.java
jc@soyuz:~/tmp/jstatic$ javac foo.java
jc@soyuz:~/tmp/jstatic$ java -classpath "." test
a set to 100

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org