No constructor initializer list in Java ?

Discussion in 'Java' started by Razvan, Jul 1, 2004.

  1. Razvan

    Razvan Guest

    Hi!




    I see that in Java there is no constructro innitializer list.
    That means you cannot do stuff like:


    class CTest
    {
    int counter;
    CTest():counter(0){}
    }

    What is the best way to innitialize attributes in Java ?



    Regards,
    Razvan
     
    Razvan, Jul 1, 2004
    #1
    1. Advertising

  2. > class CTest
    > {
    > int counter;
    > CTest():counter(0){}
    > }
    >
    > What is the best way to innitialize attributes in Java ?


    class CTest {
    int counter=0;
    CTest(){}
    }

    or:

    class CTest {
    int counter;
    CTest(){
    counter=0;
    }
    }

    Niels Dybdahl
     
    Niels Dybdahl, Jul 1, 2004
    #2
    1. Advertising

  3. Razvan wrote:

    > What is the best way to innitialize attributes in Java ?


    See other response.

    You would be well advised to actually learn Java if you plan to use it,
    instead of depending on its similarities to other languages. If you
    depend only on your knowledge of other languages then you will encounter
    problems where Java is grossly different, as in the case you asked
    about, and you will run into multiple pitfalls and write buggy code
    where Java is subtly different.

    Sun's Java Tutorial is an excellent starting point; it's "Learning the
    Java Language" trail may be all the language reference you need to come
    up to speed.

    http://java.sun.com/docs/books/tutorial/index.html


    John Bollinger
     
    John C. Bollinger, Jul 1, 2004
    #3
  4. Razvan

    Hemal Pandya Guest

    "Niels Dybdahl" <-graphics.com> writes:
    [....]
    >
    > class CTest {
    > int counter=0;
    > CTest(){}
    > }
    >
    > or:
    >
    > class CTest {
    > int counter;
    > CTest(){
    > counter=0;
    > }
    > }


    As written above, neither of this is necessary as instance variables
    are assigned a default value (0 for integer types, null for
    objects). If you want to assign them something different -- say
    initialize counter to -1 -- either of the above works; personally I
    prefer the former.
     
    Hemal Pandya, Jul 1, 2004
    #4
  5. Razvan

    Roedy Green Guest

    Roedy Green, Jul 1, 2004
    #5
  6. In article <>,
    Hemal Pandya <> wrote:
    >"Niels Dybdahl" <-graphics.com> writes:
    >[....]
    >>
    >> class CTest {
    >> int counter=0;
    >> CTest(){}
    >> }
    >>
    >> or:
    >>
    >> class CTest {
    >> int counter;
    >> CTest(){
    >> counter=0;
    >> }
    >> }

    >
    >As written above, neither of this is necessary as instance variables
    >are assigned a default value (0 for integer types, null for
    >objects). If you want to assign them something different -- say
    >initialize counter to -1 -- either of the above works; personally I
    >prefer the former.


    There is a slight difference to the two approaches if you're using
    initializer blocks, as the following code demonstrates. Whether or not
    this matters to you I don't know. This use of initializer blocks
    doesn't seem advisable anyway.


    public class Test
    {
    public static void main(String[] args)
    {
    new Before();
    new After();
    }
    }

    // Initializes the member before invoking the ctor
    class Before
    {
    public int a = 1;
    public void printA()
    {
    System.out.println("Before: " + a);
    }

    // Initializer block - gets run before ctor is invoked
    {
    printA();
    }
    }

    // Initializes the member after the ctor is invoked
    class After
    {
    public int a;
    public After()
    {
    a = 1;
    }
    public void printA()
    {
    System.out.println("After: " + a);
    }

    // Initializer block - gets run before ctor is invoked
    {
    printA();
    }
    }

    The output is:

    bash-2.04$ java Test
    Before: 1
    After: 0
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Jul 1, 2004
    #6
  7. (Razvan) writes:

    > I see that in Java there is no constructro innitializer list.
    > That means you cannot do stuff like:
    >
    >
    > class CTest
    > {
    > int counter;
    > CTest():counter(0){}
    > }


    This is ugly syntax for

    class CTest
    {
    int counter;
    CTest() {
    counter = 0
    }
    }

    "Initializer lists" are just code placed at a completely inappropriate
    place, and whoever thought of it should cut down on the medication.
     
    Tor Iver Wilhelmsen, Jul 3, 2004
    #7
  8. Tor Iver Wilhelmsen wrote:
    >
    > (Razvan) writes:
    >
    > > I see that in Java there is no constructro innitializer list.
    > > That means you cannot do stuff like:
    > >
    > >
    > > class CTest
    > > {
    > > int counter;
    > > CTest():counter(0){}
    > > }

    >
    > This is ugly syntax for
    >
    > class CTest
    > {
    > int counter;
    > CTest() {
    > counter = 0
    > }
    > }
    >
    > "Initializer lists" are just code placed at a completely inappropriate
    > place, and whoever thought of it should cut down on the medication.


    The reason initializer lists are needed is to call the constructor for local (on the
    stack) object instances. C++ has no syntax for doing that in non-declarative code.

    This is just a C++ kludge which is not needed for Java.

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
     
    Lee Fesperman, Jul 4, 2004
    #8
    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. Chris K
    Replies:
    1
    Views:
    576
    Victor Bazarov
    Apr 17, 2004
  2. ccs
    Replies:
    2
    Views:
    397
    Rolf Magnus
    Jun 7, 2004
  3. Replies:
    5
    Views:
    10,271
    Rolf Magnus
    Apr 15, 2005
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,253
  5. Pallav singh
    Replies:
    5
    Views:
    652
Loading...

Share This Page