No constructor initializer list in Java ?

R

Razvan

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
 
N

Niels Dybdahl

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
 
J

John C. Bollinger

Razvan said:
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
(e-mail address removed)
 
H

Hemal Pandya

Niels Dybdahl said:
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.
 
B

Bent C Dalager

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
 
T

Tor Iver Wilhelmsen

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.
 
L

Lee Fesperman

Tor said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top