if instance variable get initialize after assigning some values or after constructor then when does

Discussion in 'Java' started by Tony Morris, Feb 3, 2006.

  1. Tony Morris

    Tony Morris Guest

    "ganesh" <> wrote in message
    news:...
    > if instance variable get initialize after assigning some values or
    > after constructor then when does static variable get initialize


    Assuming the static field is a non-constant (JLS 15.28), the bytecode will
    perform the assignment in a static initializer - which is executed at class
    load time. If the static field is a constant, the assignment occurs at
    compile-time.

    --
    Tony Morris
    http://tmorris.net/

    Java Questions and Answers
    http://jqa.tmorris.net/
     
    Tony Morris, Feb 3, 2006
    #1
    1. Advertising

  2. Tony Morris

    ganesh Guest

    if instance variable get initialize after assigning some values or
    after constructor then when does static variable get initialize


    public class A{

    private int a;
    private int b=0;
    private Integer c;
    private Integer d=new Integer(2);

    public static int counter;
    private A(){
    }

    }

    by looking at above code one can say that variable b,d get initialize
    by assignment operator
    variable a,c by constructor to default values


    i am not aware of how does variables get initialize

    i think
    1] static variables without assignment
    2] static variables with assignment
    3] instance variables with assignment
    4] instance variables without assignment to default values

    is it right flow of variables initializtion?
     
    ganesh, Feb 4, 2006
    #2
    1. Advertising

  3. If your code would have said :
    public static int counter = 0 ;

    counter would be an initialized part of the class when it is loaded
    into memory.

    If you code would have contained this addition :
    static {
    counter=1;
    }
    counter would have been initialized right after the class has been
    loaded into memory.
    So this applies to cases, where no single instance has yet been
    created, if ever.
    More on static :
    http://javalessons.com/cgi-bin/fun/java-programming.cgi?subject=java-static&1cd=stc&sid=ao789

    ---
    Paul Hamaker, SEMM
    http://javalessons.com
     
    Paulus de Boska, Feb 4, 2006
    #3
  4. Tony Morris

    Roedy Green Guest

    On 4 Feb 2006 00:01:21 -0800, "ganesh" <> wrote,
    quoted or indirectly quoted someone who said :

    >if instance variable get initialize after assigning some values or
    >after constructor then when does static variable get initialize


    See http://mindprod.com/jgloss/initialisation.html

    Statics get initialised the first time you use a class before any
    instances of that class are created.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 4, 2006
    #4
    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. Ian Rutgers
    Replies:
    2
    Views:
    421
    Ian Rutgers
    Feb 2, 2005
  2. NewToCPP
    Replies:
    12
    Views:
    647
    Frederick Gotham
    Jul 21, 2006
  3. Replies:
    0
    Views:
    292
  4. Martin Jansson
    Replies:
    11
    Views:
    239
  5. Replies:
    4
    Views:
    545
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page