Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > No constructor initializer list in Java ?

Reply
Thread Tools

No constructor initializer list in Java ?

 
 
Razvan
Guest
Posts: n/a
 
      07-01-2004
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
 
Reply With Quote
 
 
 
 
Niels Dybdahl
Guest
Posts: n/a
 
      07-01-2004
> 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


 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      07-01-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Hemal Pandya
Guest
Posts: n/a
 
      07-01-2004
"Niels Dybdahl" <(E-Mail Removed)-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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-01-2004
On 1 Jul 2004 03:27:32 -0700, (E-Mail Removed) (Razvan) wrote or
quoted :

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


see http://mindprod.com/jgloss/initialisation.html
and http://mindprod.com/jgloss/constructor.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Bent C Dalager
Guest
Posts: n/a
 
      07-01-2004
In article <(E-Mail Removed)>,
Hemal Pandya <(E-Mail Removed)> wrote:
>"Niels Dybdahl" <(E-Mail Removed)-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 - (E-Mail Removed) - http://www.pvv.org/~bcd
powered by emacs
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      07-03-2004
(E-Mail Removed) (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.
 
Reply With Quote
 
Lee Fesperman
Guest
Posts: n/a
 
      07-04-2004
Tor Iver Wilhelmsen wrote:
>
> (E-Mail Removed) (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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initializer Vs assignment list in constructor Pallav singh C++ 5 01-18-2008 06:13 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
constructor initializer list jrefactors@hotmail.com C++ 5 04-15-2005 03:47 PM
initializer list of copy constructor ccs C++ 2 06-07-2004 01:06 AM
composition/aggregation: would like to use constructor body rather than initializer list Chris K C++ 1 04-17-2004 08:38 PM



Advertisments