Prev: What is the difference between Memory Usage and Heap Usage in my JVM Metrics ?
Next: Java Lead with GUI & SWT | CA | 10+ months
From: Pitch on 11 Feb 2010 07:07 In article <hl0bth$4ll$1(a)news.eternal-september.org>, mscottschilling(a)hotmail.com says... > > Eric Sosman wrote: > > On 2/10/2010 7:16 PM, Mike Schilling wrote: > >> Suppose the call to "super()" needn't be the first line of a > >> constructor. Discuss the effect and/or legality of the following > >> code: class MyClass extends YourClass > >> { > >> public MyClass(int i) > >> { > >> if ( i> 0) > >> { > >> super(i); > >> } > >> } > >> ... > > > > Illegal[*]: It is possible to construct a MyClass instance > > that has not constructed its YourClass aspect. > > > > [*] Well, we're already in fairy-land. But if we're to retain > > any sanity, even fairy-land must abide by sane rules. > > > > A fairy-land compiler could detect the failure to call super() > > (or the possibility of calling it more than once) with logic similar > > to that which real compilers use to detect the use of "possibly > > uninitialized" variables. > > Now that you mention it, it's almost exactly the same logic used to > determine that final fields are initialized exactly once. The only > difference is that it would also need to detect that instance fields and > methods are accessed only after that's done. Actually, read-only (final) fields are maybe the only reason they kept this rule, so they wouldn't have to check how many if the field was accessed more than once. It's a neat little rule that made lazy java designes happy. -- stirr your cofee properly
From: Pitch on 11 Feb 2010 07:09 In article <hkvi82$usn$1(a)news.eternal-september.org>, mscottschilling(a)hotmail.com says... > > Suppose the call to "super()" needn't be the first line of a constructor. > Discuss the effect and/or legality of the following code: > > class MyClass extends YourClass > { > public MyClass(int i) > { > if ( i > 0) > { > super(i); > } > } > ... If you look at super() as just another method call it's perfectly ok. Apart from the final fields which would need specific mechanism (a flag saying it is already initialized). -- stirr your cofee properly
From: Patricia Shanahan on 11 Feb 2010 10:37 Mike Schilling wrote: > Suppose the call to "super()" needn't be the first line of a constructor. > Discuss the effect and/or legality of the following code: > > class MyClass extends YourClass > { > public MyClass(int i) > { > if ( i > 0) > { > super(i); > } > } > ... > > The most complete way of dealing with this would be to have two concepts, "definitely constructed" and "definitely not constructed" that would be calculated in a similar way to "definitely assigned". At the start of the body of the constructor "definitely constructed" is false and "definitely not constructed" is true. Use of super and this to call another constructor is permitted if, and only if, "definitely not constructed" is true. References that use "this", return and fall through to the end of the constructor are permitted if, and only if, "definitely constructed" is true. Even if neither is true, completion through throw of an exception is permitted. In the example case, at the end of the if statement "definitely constructed" is false because of the i >= 0 case. "definitely not constructed is false because of the i < 0 case. There can be no more super or this calls, and the constructor must complete by throwing an exception. Constructors that follow the current standard would transition to "definitely constructed" true and "definitely not constructed" false after the first statement of the constructor body. Patricia
From: Lew on 11 Feb 2010 11:20 mscottschilling(a)hotmail.com says... >> Suppose the call to "super()" needn't be the first line of a constructor. >> Discuss the effect and/or legality of the following code: >> >> class MyClass extends YourClass >> { >> public MyClass(int i) >> { >> if ( i > 0) >> { >> super(i); >> } >> } >> ... Pitch wrote: > If you look at super() as just another method call it's perfectly ok. Constructor calls are not method calls. So that won't work. -- Lew
From: Mike Schilling on 11 Feb 2010 11:30
Pitch wrote: > In article <hkvi82$usn$1(a)news.eternal-september.org>, > mscottschilling(a)hotmail.com says... >> >> Suppose the call to "super()" needn't be the first line of a >> constructor. Discuss the effect and/or legality of the following >> code: >> >> class MyClass extends YourClass >> { >> public MyClass(int i) >> { >> if ( i > 0) >> { >> super(i); >> } >> } >> ... > > If you look at super() as just another method call it's perfectly ok. But it isn't "just another method call", which is the point of the example. It's the call that guarantees that the superclass, starting from a completely uninitialized state, has been fully initialized. It needs to be called precisely once, and that needs to be done before any instance methods are called or instance fields accessed. |