I
Ian T
LOL. Aint that the truth.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.
Ian
LOL. Aint that the truth.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.
Of course it works. And class methods and instance methods/constructors are
two different animals, if that's what you're referring to.
I'm not following you. How could a method called from a constructor resultJames 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.
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.
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.
Ulf Nordlund said: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..)
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.
I see what you're talking about. We're getting into sticky parts here. YouChris 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 certainly recommend caution, and avoiding this if possible. However,
when you say it's "not recommended", who do you mean doesn't recommend
it?
Should be, assuming you're creating an instance of B somewhere Should: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?
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".
Chris Uppal said:I agree, the better way to describe it as that the "constructor has not
finished".
Chris said:Your statement is undoubtedly true, but it sort of misses the point.
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.