A question about java swing (and java in general)

Discussion in 'Java' started by Gav, May 14, 2006.

  1. Gav

    Gav Guest

    I've seen an example like this:

    Class Foo {
    JButton one = new Jbutton("one");
    JButton two = new Jbutton("two");

    public Foo() {

    [...]
    }
    }

    How come that memers arre initialized in their declaration? Is it a
    JButton strageness? I'm a little confused.

    Thanks in advance


    GAv
    Gav, May 14, 2006
    #1
    1. Advertising

  2. Hi,

    Gav schrieb:
    > I've seen an example like this:
    >
    > Class Foo {
    > JButton one = new Jbutton("one");
    > JButton two = new Jbutton("two");
    >
    > public Foo() {
    >
    > [...]
    > }
    > }
    >
    > How come that memers arre initialized in their declaration? Is it a
    > JButton strageness? I'm a little confused.


    What excatly do you mean? Members are AFAIK the class' methods. On the
    other hand you have fields - the variables. I think you mixed up the names.

    What your examle code does is defining the class "Foo" with the two
    fields "one" and "two, both of type JButton. Both fields are initialized
    with an instance of JButton (Foo#one lables "one" and Foo#two labels
    "two"). Then follows the class' constructor.

    That's pretty normal Java to me. You can initialize a field where it is
    definied or you can leaf that. Object types defaultly initialize with
    null, number value types (int, long, double, ...) with 0 and boolean
    with false. However, I think it's better to set a defined initialization
    value for fields to prevent predictable programm behaviour.


    >
    > Thanks in advance
    >
    >
    > GAv


    hth,
    Tobi
    =?ISO-8859-15?Q?Tobias_Schr=F6er?=, May 14, 2006
    #2
    1. Advertising

  3. Gav

    Gav Guest

    Tobias Schröer wrote:
    > Hi,
    >
    > Gav schrieb:
    >> I've seen an example like this:
    >>
    >> Class Foo {
    >> JButton one = new Jbutton("one");
    >> JButton two = new Jbutton("two");
    >>
    >> public Foo() {
    >> [...]
    >> }
    >> }
    >>
    >> How come that memers arre initialized in their declaration? Is it a
    >> JButton strageness? I'm a little confused.

    >
    > What excatly do you mean? Members are AFAIK the class' methods. On the
    > other hand you have fields - the variables. I think you mixed up the names.


    YEs, sorry. Of course I meant fields.


    > That's pretty normal Java to me. You can initialize a field where it is
    > definied or you can leaf that. Object types defaultly initialize with
    > null, number value types (int, long, double, ...) with 0 and boolean
    > with false. However, I think it's better to set a defined initialization
    > value for fields to prevent predictable programm behaviour.


    Shouldn't this things be done in class constructor?

    Thanks again,
    Gav
    Gav, May 14, 2006
    #3
  4. Gav

    Bjorn Abelli Guest

    "Gav" wrote

    [snip]

    >>> Class Foo {
    >>> JButton one = new Jbutton("one");
    >>> JButton two = new Jbutton("two");
    >>>
    >>> public Foo() {
    >>> [...]
    >>> }
    >>> }


    [snip]

    > Shouldn't this things be done in class constructor?


    Why "should" they?

    There are several possible places where instance fields can be initialized,
    and where to initialize them is most of the time a question of taste,
    personal preferences and workshop standards (which can differ between
    firms).

    Some want to see all instance fields initialized at one place, the
    constructor, others don't want to clutter up the constructor with
    initializations that can be done at the point of declaration.

    It would be worse if they initialized them at the declaration *and* assigned
    values to them in the constructor.

    // Bjorn A




    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
    Bjorn Abelli, May 14, 2006
    #4
  5. Tobias Schröer wrote:

    > Gav schrieb:
    >> I've seen an example like this:
    >>
    >> Class Foo {
    >> JButton one = new Jbutton("one");
    >> JButton two = new Jbutton("two");
    >>
    >> public Foo() {
    >> [...]
    >> }
    >> }
    >>
    >> How come that memers arre initialized in their declaration? Is it a
    >> JButton strageness? I'm a little confused.


    This is a feature of the Java language. It is as though the
    initialization code were inserted automatically at the beginning of
    every constructor. You can do this with any member variables. You also
    can provide initializers for static members right where they are
    defined, and the initialization code will be run when the class is loaded.


    > What excatly do you mean? Members are AFAIK the class' methods. On the
    > other hand you have fields - the variables. I think you mixed up the names.


    Both fields and methods are members in Java.
    Jeffrey Schwab, May 15, 2006
    #5
  6. Hi,

    Jeffrey Schwab schrieb:
    > Tobias Schröer wrote:
    >
    >> Gav schrieb:
    >>
    >>> I've seen an example like this:
    >>>
    >>> Class Foo {
    >>> JButton one = new Jbutton("one");
    >>> JButton two = new Jbutton("two");
    >>>
    >>> public Foo() {
    >>> [...]
    >>> }
    >>> }
    >>>
    >>> How come that memers arre initialized in their declaration? Is it a
    >>> JButton strageness? I'm a little confused.

    >
    >
    > This is a feature of the Java language. It is as though the
    > initialization code were inserted automatically at the beginning of
    > every constructor.


    Right. If you follow the call sequence with a debugger, it looks like this:

    - call super constructor and initialize super type
    - step through field declarations (and assignments)
    - execute rest of constructor

    > You can do this with any member variables. You also
    > can provide initializers for static members right where they are
    > defined, and the initialization code will be run when the class is loaded.


    That's how constants (static final int CONST_INT = 1;) are typically
    initialized. Though, you should be able to set their values in the
    static initializer, too. But that would be rather confusing, regarding
    the extra code you'll have to write.

    >> What excatly do you mean? Members are AFAIK the class' methods. On the
    >> other hand you have fields - the variables. I think you mixed up the
    >> names.

    >
    >
    > Both fields and methods are members in Java.


    That's right. It seems that *I* mixed up the terms a bit :(

    Tobi
    =?ISO-8859-15?Q?Tobias_Schr=F6er?=, May 15, 2006
    #6
  7. Gav

    Gav Guest

    Tobias Schröer wrote:
    > Hi,
    >

    [...]

    Thanks all for the answers, now it's clearer.


    Gav
    Gav, May 15, 2006
    #7
    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. Gertraud
    Replies:
    2
    Views:
    395
    Gertraud
    Mar 17, 2005
  2. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    485
    Pete Barrett
    Apr 16, 2005
  3. mkrause
    Replies:
    0
    Views:
    654
    mkrause
    May 6, 2005
  4. lizard
    Replies:
    0
    Views:
    1,745
    lizard
    Jan 30, 2006
  5. S.T
    Replies:
    2
    Views:
    555
Loading...

Share This Page