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 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »

Posts:19,725
Registered: 9/26/01
Re: java constructors drive me mad  
Jun 17, 2004 11:24 AM (reply 15 of 219)



 
The compiler could do a better job telling us about the mistakes we make.

That's the key. The mistake you (valjok) are making by writing your code in such an ill-conceived way and not seeing that it is ill-conceived.
 

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 17, 2004 11:24 AM (reply 16 of 219)



 
My logic is that using an uninitialized field
is a syntax error.
Where do I have the uninitialized variable?
    class Tree extends JTree {        private final DefaultMutableTreeNode root =  new DefaultMutableTreeNode("Download Manager");        Tree() {            super(new DefaultTreeModel(root));        }


Furthermore, I
explicitly initialize the root var in the constructor
before passing it to the super-constructor.

No. If you read [url http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44670]12.5 Creation of New Class Instances[/url] in the JLS, you'll see that super's ctor runs before the initialization that you provide for root. The reason, presumably, is that you want the parent to be fully initialized before doing anything in the child.

 

Posts:11,200
Registered: 7/22/99
Re: java constructors drive me mad  
Jun 17, 2004 11:30 AM (reply 17 of 219)



 
A bug (omission) in the official langspec, see here:
http://www.ergnosis.com/java-spec-report/java-language/
ls-12.5.html

According to that finals and static finals should be treated the stame, I guess that then the end of section13.4.8 in the langspec applies to final instance variables as well?

quote:
One other thing to note is that static final fields that have constant values (whether of primitive or String type) must never appear to have the default initial value for their type (�4.5.5). This means that all such fields appear to be initialized first during class initialization (�8.3.2.1, �9.3.1, �12.4.2).
 

Posts:835
Registered: 2/12/01
Re: java constructors drive me mad  
Jun 17, 2004 11:37 AM (reply 18 of 219)



 
My logic is that using an uninitialized field
is a syntax error.
Where do I have the uninitialized variable?
class Tree extends JTree {private final DefaultMutableTreeNode root =  newDefaultMutableTreeNode("Download Manager");Tree() {super(new DefaultTreeModel(root));}


Furthermore, I
explicitly initialize the root var in the
constructor
before passing it to the super-constructor.

No. If you read [url
http://java.sun.com/docs/books/jls/second_edition/html/
xecution.doc.html#44670]12.5 Creation of New Class
Instances
[/url] in the JLS, you'll see that
super's ctor runs before the initialization
that you provide for root. The reason, presumably, is
that you want the parent to be fully initialized
before doing anything in the child.

?

Do you indentionally ignore my first example where I explicitly initialize the "root" before passing the same reference to super?

class Tree extends JTree {        private final DefaultMutableTreeNode root;        Tree() {            super(new DefaultTreeModel(root = new DefaultMutableTreeNode("Download Manager")));                  }
 

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 17, 2004 11:42 AM (reply 19 of 219)



 
Do you indentionally ignore my first example where I
explicitly initialize the "root" before passing
the same reference to super?
class Tree extends JTree {private final DefaultMutableTreeNode root;Tree() {super(new DefaultTreeModel(root = newDefaultMutableTreeNode("Download Manager")));}

Did you intentionally ignore the part in the JLS that says that doesn't matter? Super's ctor is caled first. I saw your code. I see that in the source "root = new..." occurs before "super(..." but the spec says it's not executed in that order.
 

Posts:37,103
Registered: 3/30/99
Re: java constructors drive me mad  
Jun 17, 2004 11:50 AM (reply 20 of 219)



 
Do you indentionally ignore my first example where I
explicitly initialize the "root" before passing
the same reference to super?
class Tree extends JTree {private final DefaultMutableTreeNode root;Tree() {super(new DefaultTreeModel(root = newDefaultMutableTreeNode("Download Manager")));}

Okay, I misread which example you were using here. This is probably spelled out elsewhere, but I think it's the same thing--you can't reference child class fields before the parent is completely constructed.
 

Posts:11,200
Registered: 7/22/99
Re: java constructors drive me mad  
Jun 17, 2004 11:56 AM (reply 21 of 219)



 
Correct; you are trying to assign to "root" before the superclass constructor has run; so this is not allowed. Like I said in my first post in this thread there is a workaround but it's not nice and should be avoided.

For those who are interested, check out issues 086 and 086b of the The Java Specialists' Newsletter at javaspecialists.co.za
 

Posts:835
Registered: 2/12/01
Re: java constructors drive me mad  
Jun 17, 2004 1:07 PM (reply 22 of 219)



 
jverd, you're right. Forgive me for being a little offencive, this is from such irresponsible statements like

(warneja): Java won't let me code in my wonky way! Wah!
That is what I'm calling "blind" beleiver. Everything that is written in JLS (compiler) is the absolute true for them. Thanks to jsalonen who approves my doubts about bug (omission) in the official langspec.
 

Posts:11,200
Registered: 7/22/99
Re: java constructors drive me mad  
Jun 17, 2004 1:13 PM (reply 23 of 219)



 
nah, warnerja's comment was just a joke, I doubt s/he's a blind believer :)
 

Posts:835
Registered: 2/12/01
Re: java constructors drive me mad  
Jun 17, 2004 1:20 PM (reply 24 of 219)



 
nah, warnerja's comment was just a joke, I doubt
s/he's a blind believer :)

May be a joke, but repeated twice without explanation is annoying.


The compiler could do a better job telling us about the mistakes we make.
That's the key. The mistake you (valjok) are making by writing your code in such an ill-conceived way and not seeing that it is ill-conceived.


I'd agree that java compiler is a perfect helper though.
 

Posts:19,725
Registered: 9/26/01
Re: java constructors drive me mad  
Jun 17, 2004 1:27 PM (reply 25 of 219)



 
But you are in fact writing it in a wonky way. No, it was no joke. You are expecting to construct your subclass, or at least part of it, before constructing the base. That's wonky indeed. Do you actually think you're being validated by the other posters?
 

Posts:11,200
Registered: 7/22/99
Re: java constructors drive me mad  
Jun 17, 2004 1:35 PM (reply 26 of 219)



 
Thanks to jsalonen who approves my doubts about bug (omission) in
the official langspec.

Just to check.. which doubts do I approve? The "omission" I mentioned has to do with the irregularity that DrLaszloJamf discovered with final variables that are initilalized with compiletime constant expressions. I has got nothing to do with your original problem.

But yes, in general there are errors in the specification.
 

Posts:835
Registered: 2/12/01
Re: java constructors drive me mad  
Jun 17, 2004 2:55 PM (reply 27 of 219)



 
I just see the irregularity in the fact that it is not allowed to access initialized variables (and it does not eliminate the probability to use not initialized var).
 

Posts:835
Registered: 2/12/01
Re: java constructors drive me mad  
Jun 17, 2004 3:32 PM (reply 28 of 219)



 
You are expecting to construct
your subclass, or at least part of it, before
constructing the base. That's wonky indeed.

IMO, subclass is nothing more that extention (additional fields). Constucting means filling object fields with data. What is the fundamental problem to reverse the initialization sequence:
Super.X=10
Super.Y=20
this=30

Generally speaking, "base" is an independent object that something relies on. Independent object (variable) is a base for another refering to (assigning) it. Building my constrution from known blocks I will always succeed.

 

Posts:6,750
Registered: 1/25/04
Re: java constructors drive me mad  
Jun 17, 2004 3:36 PM (reply 29 of 219)



 
Well, subclasses could rely on superclass fields to function properly, but superclasses should never rely on subclasses to function properly. So there could be situations where it's important to initialize the superclass first, but there should never be any need to initialize the subclass first.
 
This topic has 219 replies on 15 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »