Home arrow static arrow Java Programming [Archive] - java constructors drive me mad
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java constructors drive me mad
This topic has 219 replies on 15 pages.    « Previous | 1 | ... 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Next »

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 21, 2004 9:12 AM (reply 165 of 219)



 
You really are a **** head aren't you.

Especially for you I have repeated it 1000 times.

Fields of a record must be independent and can be
populated in ANY order.

As I pointed out earlier, it's not necessarily that simple. To just make the above statement and expect that to be sufficient is to take a very naive view of the overall construction process. It may in fact be the case that they could be populated in any order, but you'll have to actually provide some evidence that the complexities of construction of super- and sub-classes in general don't either negate your claim or make it irrelevant.
 

Posts:4,906
Registered: 23/07/02
Re: java constructors drive me mad  
Jun 21, 2004 10:43 AM (reply 166 of 219)



 
I'm not diplomatic;

Really? Hadn't noticed... ;p

therefore, it often prevents me to deliver correct points to mistaken people.

And there I was thinking that that was because you were a troll and didn't have any correct points. Thanks for correcting me on that.
 

Posts:11,186
Registered: 06.04.04
Re: java constructors drive me mad  
Jun 21, 2004 11:46 AM (reply 167 of 219)



 
Fields of a record must be independent and can be populated in ANY order.

As I pointed out earlier, it's not necessarily that simple. To just make the above statement and expect
that to be sufficient is to take a very naive view of the overall construction process. It may in fact be
the case that they could be populated in any order, but you'll have to actually provide some evidence that
the complexities of construction of super- and sub-classes in general don't either negate your claim
or make it irrelevant.

Valjok does have a point though: the compiler does all it can to ensure that superclass constructors
are called first and no current class member variables can be accessed before the superclass
is fully initialized, while at the same time (I'm talking Java now), a simple trick does allow one to
set/get those supposedly uninitialized variables. (the superclass constructor calling a method that's
been overridden in the subclass; the latter sets/gets those member fields).

A valid question would be: why does the compiler run the whole nine yards while it's 'protection'
(mind the quotes) can easily be circumvented? I'd only wish that that discussion could be carried
on a bit more friendly ... ;-)

kind regards,

Jos
 

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 21, 2004 12:51 PM (reply 168 of 219)



 
Valjok does have a point though: the compiler does all
it can to ensure that superclass constructors
are called first and no current class member variables
can be accessed before the superclass
is fully initialized, while at the same time (I'm
talking Java now), a simple trick does allow one to
set/get those supposedly uninitialized variables. (the
superclass constructor calling a method that's
been overridden in the subclass; the latter sets/gets
those member fields).

A valid question would be: why does the compiler run
the whole nine yards while it's 'protection'
(mind the quotes) can easily be circumvented?

Seems like it would be one of the following:

a) The designers made an error in making it too restrictive.

b) They made an error in not making it restrictive enough be allowing the above workaround

c) The distinction is deliberate, because of subtle differences between the workaround and what valjok wants that we're not seeing yet.

d) The distinction is deliberate, simply for practical reasons--for example, maybe it doesn't need to be as strict as it is, but to allow what valjok wants would complicate compilers or VMs too much for too little gain.

I'd only wish that that discussion could be carried
on a bit more friendly ... ;-)

Your chin is my ballrest. ;-)
 

Posts:1,577
Registered: 00-02-25
Re: java constructors drive me mad  
Jun 21, 2004 1:56 PM (reply 169 of 219)



 
A valid question would be: why does the compiler run
the whole nine yards while it's 'protection'
(mind the quotes) can easily be circumvented?

I don't think there's any "protection" that the compiler can give you. When it compiles a method call, it simply verifies that the method is found in the symbol table, then generates an "invokevirtual" operation. I suppose that a compiler could perform the additional check that (1) the method is not abstract, and (2) it's not defined by a superclass, but this still wouldn't prevent you from overriding the method in a subclass. So, it's up to the programmer to know how Java object construction takes place.

As for the "layered" approach to object construction, this makes perfect sense from an OOP perspective. Every object is its superclass, not vice-versa. Therefore, the superclass should be fully constructed before any of the subclass' construction takes place (one well-known author who I can't remember at the moment describes this as establishing the class invariant before operations are performed on the class).

Java simply takes this philosophy into practice, and initializes/constructs (they are different!) each layer in order. Perhaps not what a C++ programmer expects, but them's the rules.
 

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 21, 2004 2:08 PM (reply 170 of 219)



 
As for the "layered" approach to object construction,
this makes perfect sense from an OOP perspective.
Every object is its superclass, not
vice-versa. Therefore, the superclass should be fully
constructed before any of the subclass'
construction takes place

I agree that the superclass should be fully constructed before you do anything with the subclass, but I don't see how that follows from "Every object is its superclass."
 

Posts:1,577
Registered: 00-02-25
Re: java constructors drive me mad  
Jun 21, 2004 2:17 PM (reply 171 of 219)



 
I agree that the superclass should be fully
constructed before you do anything with the subclass,
but I don't see how that follows from "Every object is
its superclass."

The point I was trying to make was to differentiate between a subclass calling methods defined by its superclass, and a superclass calling methods defined by the subclass. If the latter happens during superclass construction, you're creating an unnatural relationship between superclass and subclass.

Incidentally, I found the reference that I was thinking of re layered construction. Turns out it was Stroustrup: "What is it that makes the object a valid object? An invariant allows you to say when the object's representation is good and when it isn't." (http://www.artima.com/intv/goldilocksP.html)

Restating my last post (I think), the process of construction is hierarchical, with each class in the inheritance tree establishing whatever invariants it defines before any initialization/construction happens for subclasses.
 

Posts:11,186
Registered: 06.04.04
Re: java constructors drive me mad  
Jun 21, 2004 2:19 PM (reply 172 of 219)



 
A valid question would be: why does the compiler run the whole nine yards while it's 'protection'
(mind the quotes) can easily be circumvented?

I don't think there's any "protection" that the compiler can give you. When it compiles a method call,
it simply verifies that the method is found in the symbol table, then generates an "invokevirtual"
operation. I suppose that a compiler could perform the additional check that (1) the method is not
abstract, and (2) it's not defined by a superclass, but this still wouldn't prevent you from overriding the
method in a subclass. So, it's up to the programmer to know how Java object construction takes place.

True, all true; but a corollary of my hypothetical question would be then: why not remove the compile
time restriction and allow for (dangerous) constructs like this --
class B {   int x, y;   B(int z) { x= foo(z); }}class D {   D() { y= 42; super(1); }   void foo(int z) { return y+1; }} 


IOW, the compiler can't prevent such idiocy, so why the semantical restriction by imposing such
a lexical restriction? (I feel I'm playing devils advocate here)

As for the "layered" approach to object construction, this makes perfect sense from an OOP perspective.
Every object is its superclass, not vice-versa. Therefore, the superclass should be fully
constructed before any of the subclass' construction takes place (one well-known author who I
can't remember at the moment describes this as establishing the class invariant before operations are
performed on the class).

Sure, it makes sense, but Java doesn't enforce that notion although the compiler tries to do more than
its best ...

Java simply takes this philosophy into practice, and initializes/constructs (they are different!) each
layer in order. Perhaps not what a C++ programmer expects, but them's the rules.

But them rules are too easily circumvented in Java, so why not take that (failing) complexity away
from the language (read: the compiler) itself? It would be a nice question to think about ... ;-)

kind regards,

Jos
 

Posts:11,186
Registered: 06.04.04
Re: java constructors drive me mad  
Jun 21, 2004 2:22 PM (reply 173 of 219)



 
Oops! lexical == syntactical

Jos (it's late overhere ...)
 

Posts:6,750
Registered: 1/25/04
Re: java constructors drive me mad  
Jun 21, 2004 2:34 PM (reply 174 of 219)



 
I don't think there's any "protection" that the
compiler can give you. When it compiles a method call,
it simply verifies that the method is found in the
symbol table, then generates an "invokevirtual"
operation. I suppose that a compiler could perform the
additional check that (1) the method is not abstract,
and (2) it's not defined by a superclass, but this
still wouldn't prevent you from overriding the method
in a subclass.

Well, it could prevent you from calling any non-final methods.

So, it's up to the programmer to know
how Java object construction takes place.

Yes, and will almost surely remain so.
 

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 21, 2004 2:35 PM (reply 175 of 219)



 
Restating my last post (I think), the process of
construction is hierarchical, with each class in the
inheritance tree establishing whatever invariants it
defines before any initialization/construction
happens for subclasses.

I agree--and to be clear, I think this is a general quality of good OO, not just something enforced by Java. The problem is I can't say why. It just feels right. Maybe after I have a chance to read that article I'll have something more than my gut to go on here.
 

Posts:6,750
Registered: 1/25/04
Re: java constructors drive me mad  
Jun 21, 2004 2:37 PM (reply 176 of 219)



 
But them rules are too easily circumvented in Java, so
why not take that (failing) complexity away
from the language (read: the compiler) itself? It
would be a nice question to think about ... ;-)

It's been suggested that it is simpler to write the compiler this way, not more complex. It also rules out some evilness such as your last example. The fact that it allows other evilness doesn't mean the whole thing should be thrown out.
 

Posts:205
Registered: 6/15/04
Re: java constructors drive me mad  
Jun 21, 2004 3:25 PM (reply 177 of 219)



 

As for the "layered" approach to object construction,
this makes perfect sense from an OOP perspective.
Every object is its superclass, not
vice-versa. Therefore, the superclass should be fully
constructed before any of the subclass'
construction takes place (one well-known author who I
can't remember at the moment describes this as
establishing the class invariant before operations are
performed on the class).

I'm just shocked and amazed that valjok, isn't here telling you how "stupid" you are for spouting this "nonsense" ... I remember saying this stuff and being labelled a poor lost boy.

And remember, he knew all this stuff long before you became a (Java) programmer.

-is that my handbag we can say waving around?
 

Posts:205
Registered: 6/15/04
Re: java constructors drive me mad  
Jun 21, 2004 3:26 PM (reply 178 of 219)



 
-is that my handbag we can say waving around?

-is that my handbag we can see waving around?
 

Posts:2,830
Registered: 9/1/03
Re: java constructors drive me mad  
Jun 21, 2004 3:44 PM (reply 179 of 219)



 
I agree, because that class has been initialised
...
in our case we are assigning
fields in a class that has not been
initialised
.

What do you mean "a class that has not been
initialized"? I'm not sure what it means to
"initialize a class." Can you be more specific about
what exactly that entails?

having the variables set and any class initialisers run, thats what I mean ..,
what do you understand to happen when a class is initialised ? or am I making
this term up (its not uncommon from me ... ;)

 
This topic has 219 replies on 15 pages.    « Previous | 1 | ... 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Next »