[noob]static blocks query

Discussion in 'Java' started by mmu2643@gmail.com, Sep 10, 2005.

  1. Guest

    Hi,

    I was just trying to understand the use of 'static' for blocks of code
    and wrote the following bit of code:

    <code>
    class frodo
    {
    static int frodoCount;

    public frodo(){

    boolean init;

    init=false;

    static {
    frodoCount = 1;
    init=true;
    }

    if(!init)
    frodoCount++;

    System.out.println("Count = "+frodoCount);
    }
    };

    class gollum
    {
    public static void main(String[] args)
    {
    System.out.println("Hello World!");

    frodo a = new frodo();
    frodo b = new frodo();
    frodo c = new frodo();
    frodo d = new frodo();
    }
    }
    </code>

    But I keep getting the following compiler error:

    C:\>javac gollum.java
    gollum.java:11: illegal start of expression
    static {
    ^
    1 error

    Can someone please explain what I am doing wrong? TIA.
    , Sep 10, 2005
    #1
    1. Advertising

  2. writes:

    > I was just trying to understand the use of 'static' for blocks of code
    > and wrote the following bit of code:

    ....
    > Can someone please explain what I am doing wrong? TIA.


    Static blocks must be directly inside the class, not inside a method.
    E.g.,
    ---
    class FooSquare {
    private static final int[] preCalc = new int[100];
    static {
    for (int i = 0; i < 100; i++) {
    preCalc = i*i;
    }
    }

    public int square(int i) {
    if (i >= 0 && i < 100) {
    return preCalc;
    } else {
    return i*i;
    }
    }
    }
    ---

    A more practical example is ofcourse possible :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 10, 2005
    #2
    1. Advertising

  3. Guest

    Lasse Reichstein Nielsen wrote:
    > writes:
    >
    > > I was just trying to understand the use of 'static' for blocks of code
    > > and wrote the following bit of code:

    > ...
    > > Can someone please explain what I am doing wrong? TIA.

    >
    > Static blocks must be directly inside the class, not inside a method.


    So how/when is the code in a static block executed?

    For example:

    class a {
    static {
    code_in_static
    }

    public a(){}
    }

    When instantiating 'a' when would the static code run? It would be
    great if you could provide links to any articles that explain static
    blocks. TIA.
    , Sep 10, 2005
    #3
  4. <> wrote
    > So how/when is the code in a static block executed?

    It is automatically called once when your class is loaded, before any
    constructor or non-static method of this class is called.
    >
    > For example:
    >
    > class a {
    > static {
    > code_in_static
    > }
    >
    > public a(){}
    > }
    >
    > When instantiating 'a' when would the static code run?

    It /has/ already been called then. See above.

    > It would be
    > great if you could provide links to any articles that explain static
    > blocks. TIA.
    >

    You find some here:
    http://www.google.com/search?q=java "+static initializer"

    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Sep 10, 2005
    #4
  5. Roedy Green Guest

    Roedy Green, Sep 10, 2005
    #5
  6. Roedy Green Guest

    Roedy Green, Sep 10, 2005
    #6
  7. writes:

    > So how/when is the code in a static block executed?


    When the class is loaded, just like initalizers of static fields.

    E.g.:

    class A {
    static int a = 42;
    }

    is equivalent to

    class A {
    static int a;
    static {
    a = 42;
    }
    }


    > When instantiating 'a' when would the static code run?


    Far earlier, when the class was first loaded.

    See
    <URL:http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.7>

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 10, 2005
    #7
  8. Roedy Green coughed up:
    > On 10 Sep 2005 12:19:05 -0700, wrote or quoted :
    >
    >> gollum.java:11: illegal start of expression
    >> static {

    >
    > static blocks have to be defined outside methods. You can however
    > define a static class (nested class) inside a method.
    >
    > see http://mindprod.com/jgloss/static.html
    > http://mindprod.com/jgloss/nestedclasses.html



    This one seems fairly well put together:

    http://www.flipcode.com/articles/article_innerclasses.shtml


    By the way, Roedy, are you against having a dropdown box that changes
    bracketting styles?

    K&R
    vs.
    On next line before indent
    vs.
    On next line at indent level (your way)



    --
    Unix users who vehemently argue that the "ln" command has its arguments
    reversed do not understand much about the design of the utilities. "ln
    arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
    Existing file argument to non-existing argument. And in fact, mv
    itself is implemented as a link followed by an unlink.
    Thomas G. Marshall, Sep 11, 2005
    #8
  9. Roedy Green Guest

    On Sat, 10 Sep 2005 23:58:48 GMT, "Thomas G. Marshall"
    <> wrote or quoted
    :

    >
    >By the way, Roedy, are you against having a dropdown box that changes
    >bracketting styles?


    most definitely not. See http://mindprod.com/projects/scid.html

    I go much further than that is allowing user configurability of how
    they like to view source.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 11, 2005
    #9
  10. Roedy Green coughed up:
    > On Sat, 10 Sep 2005 23:58:48 GMT, "Thomas G. Marshall"
    > <> wrote or quoted
    >>

    >
    >>
    >> By the way, Roedy, are you against having a dropdown box that changes
    >> bracketting styles?

    >
    > most definitely not. See http://mindprod.com/projects/scid.html
    >
    > I go much further than that is allowing user configurability of how
    > they like to view source.


    I don't see where you allow for your code to be viewed differently. I'm not
    talking about any particular ide, I'm talking about your website.


    --
    "Realtor" and "realty" are pronounced "reel'-tor" and
    "reel'-tee", *not* "reel'-a-tor" and "reel'-i-tee" !!!!
    If you pronounce them with the extra syllable, you will
    sound like a complete idiot.
    Thomas G. Marshall, Sep 11, 2005
    #10
    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. Arjen
    Replies:
    3
    Views:
    439
    Scott Allen
    Feb 27, 2005
  2. Learner
    Replies:
    1
    Views:
    984
    Marina Levit [MVP]
    Jan 30, 2006
  3. anti-guru
    Replies:
    2
    Views:
    7,825
    anti-guru
    Sep 2, 2004
  4. matt
    Replies:
    1
    Views:
    258
    George Ogata
    Aug 6, 2004
  5. Steven Taylor
    Replies:
    9
    Views:
    249
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page