Can Java Programmer Learn C++ Quickly?

I

Ian T

James said:
Typically, you'll have no problem during any of your tests, but it will
segfault consistently at the demo in front of the important customer.
LOL. Aint that the truth.

Ian
 
J

James Kanze

Of course it works. And class methods and instance methods/constructors are
two different animals, if that's what you're referring to.

No. I'm referring to exactly what I said: that in Java, if you call a
virtual function from the constructor, you are very likely to end up in
a function in a class whose constructor has not even started. With,
typically, a NullPointerException as a result.
 
R

Ryan Stewart

James Kanze said:
No. I'm referring to exactly what I said: that in Java, if you call a
virtual function from the constructor, you are very likely to end up in
a function in a class whose constructor has not even started. With,
typically, a NullPointerException as a result.
I'm not following you. How could a method called from a constructor result
in the VM attempting to invoke a method on an object that doesn't exist? I
think that's what you're saying anyway. I'm not sure exactly how to put it.
 
C

Chris Smith

Ryan Stewart said:
I'm not following you. How could a method called from a constructor result
in the VM attempting to invoke a method on an object that doesn't exist? I
think that's what you're saying anyway. I'm not sure exactly how to put it.

James spoke of calling a method of an object whose constructor had not
started to run. The object does exist. See this example:

class A
{
A()
{
someMethod();
}

void someMethod()
{
}
}

class B extends A
{
private int c = 5;

B()
{
System.out.println("Beginning of B's constructor");
}

void someMethod()
{
System.out.println("B.someMethod");
System.out.println(c);

}
}

Take a guess at the output of that code, and then try it out.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Chris Smith said:
James spoke of calling a method of an object whose constructor had not
started to run. The object does exist. See this example:

I should point out that I don't agree with James' assessment that this
means that it doesn't work; it does work, and just has some surprising
results. I have even used this technique in the past, but it's
necessary to document clearly that this is occurring, so that the
implementation of the method can avoid accessing object state.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
U

Ulf Nordlund

Chris Smith skrev:
I should point out that I don't agree with James' assessment that this
means that it doesn't work; it does work, and just has some surprising
results. I have even used this technique in the past, but it's
necessary to document clearly that this is occurring, so that the
implementation of the method can avoid accessing object state.

Yes it works, but it is not recommended. I think the general rule is:
Don't call non-private methods in the constructor.

But you said that you have actually used this technique. To solve what?
(I'm just curious..)

/ulf
 
C

Chris Smith

Ulf Nordlund said:
Yes it works, but it is not recommended.

I certainly recommend caution, and avoiding this if possible. However,
when you say it's "not recommended", who do you mean doesn't recommend
it?

Ideally, there are two possible reasons to call an instance method: the
first is to get access to instance state, and the second is to perform
some task that is polymorphically bound to the class of the object. The
latter is perfectly valid in a method called from a constructor, so long
as it's not combined with the first. It's both at once that causes
problems.
I think the general rule is:
Don't call non-private methods in the constructor.

Generally non-private and non-final is the rule of thumb; private
methods are, in fact, okay only because they are effectively final
(which is not true in C++, so that is quite relevant in this thread).
But you said that you have actually used this technique. To solve what?
(I'm just curious..)

Without getting into irrelevant details, the task had to do with the
need to choose samples out of a specific random distribution, the nature
of which depended on the concrete implementation.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Joona I Palaste

James spoke of calling a method of an object whose constructor had not
started to run. The object does exist. See this example:
class A
{
A()
{
someMethod();
}
void someMethod()
{
}
}
class B extends A
{
private int c = 5;
B()
{
System.out.println("Beginning of B's constructor");
}
void someMethod()
{
System.out.println("B.someMethod");
System.out.println(c);

Take a guess at the output of that code, and then try it out.

Without testing the code or looking at the follow-ups, I hazard a guess:

B.someMethod
0
Beginning of B's constructor

Am I right?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 
R

Ryan Stewart

Chris Smith said:
I should point out that I don't agree with James' assessment that this
means that it doesn't work; it does work, and just has some surprising
results. I have even used this technique in the past, but it's
necessary to document clearly that this is occurring, so that the
implementation of the method can avoid accessing object state.
I see what you're talking about. We're getting into sticky parts here. You
and James both say the "constructor [has] not started". I would suggest that
it has, at least in the scenario you gave. I also agree that you can get
some surprising results when misusing or not being careful with
constructors, but that it doesn't mean that something is broken.
 
U

Ulf Nordlund

Chris Smith skrev:
I certainly recommend caution, and avoiding this if possible. However,
when you say it's "not recommended", who do you mean doesn't recommend
it?

Sun, for instance, in their "Advanced Java" course material (course
SL-275).
/ulf
 
R

Ryan Stewart

Joona I Palaste said:
Without testing the code or looking at the follow-ups, I hazard a guess:

B.someMethod
0
Beginning of B's constructor

Am I right?
Should be, assuming you're creating an instance of B somewhere :) Should:
1) Allocate memory for a B
2) Init member variables to defaults
3) Implicit call to A()
4) Begin body of A()
5) Execute someMethod()
6) Finish A()
7) Init c to 5
8) Begin body of B()
9) Print "Beginning of B's constructor" (maybe better to say constructor
body)
10) Finish B()

Right?
 
C

Chris Uppal

Ryan said:
I see what you're talking about. We're getting into sticky parts here. You
and James both say the "constructor [has] not started".

I agree, the better way to describe it as that the "constructor has not
finished".

With some minor quibbles about what instance intialisers mean[*], the verifier
will ensure that no part of the state of any object can be observed until the
body of its constructor has been entered, and indeed those of all its
superclass constructors too.

Incidentally one example of a valid call to a deliberately non-final method
from a constructor (which I've given before here): If you have a reset()
method that returns an object to some known clean state (which is very likely
to be overridable /by design/), then it makes a lot of sense to call that from
the constructor. The alternative may well be duplication of the initialisation
code, which is significantly more dangerous, IMO, than relying on a moderately
well-known (or not /obscure/ anyway, not even particularly counter-intuitive),
well-defined, corner-case in the JLS2.

([*] and assuming that my memory of the verifier "specification" is reliable at
this time on a Monday morning ;-)

-- chris
 
C

Chris Smith

Chris Uppal said:
I agree, the better way to describe it as that the "constructor has not
finished".

Your statement is undoubtedly true, but it sort of misses the point.
The whole point of saying that the constructor has not yet started is to
point out that no initialization will have occurred, beyond the setting
of all attributes to default values of 0, null, or false. If you
consider the superclass constructor call to be part of the constructor
(which I generally don't) then you could say something more complex,
like "before any part of the constructor has run except its superclass
constructor invocation."

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Uppal

Chris said:
Your statement is undoubtedly true, but it sort of misses the point.

Are you suggesting that /pedantry has no place on Usenet/ ?!?

Actually there was a point to that bit of my post, indeed two of them, which I
suppose I could have put more emphasis on, but I utterly refute and deny the
idea that any such emphasis was necessary ;-)

Still...

One is is that the thread (overall) has wandered into places where posters are
asking for clarification of technical details. In such circumstances it seems
(to me) obligatory that we dispense with approximation.

The other is that -- from a staightforward "how to structure the code" POV --
it's important to realise that the object can be (and typically will be)
/partially/ initialised at the time when an overriding method is called.

-- chris
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top