• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class D {
{System.out.print("1");}
static {System.out.print("2");}
D() {System.out.print("3");}
D(String s) {System.out.print("4");} // l3
}
class E extends D {
{System.out.print("5");}
static {System.out.print("6");}
// E() {System.out.print("7");this("e");} // l1
E() {this("e");System.out.print("7");} // l2
E(String s) {System.out.print("8");}
}
class F {
public static void main(String[] args) {
new E();
}
}
If I comment l1 its compile error free and
1.output is 2613587?
2.Can anyone run me through why the output is 2613587?
3.Why compile error at l1?
4. Why is it not executing l3?
Thanks
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh,
For your ques 3 -
JLS 15.8.3 -
.........The keyword this is also used in a special explicit constructor invocation statement, which can appear at the beginning of a constructor body.
If this ( & also super ) is to be present in the body of a constructor it should be the first statement.
For your ques 1 & 2
Now , all static things are taken into account during class load time. So when E is first loaded it'll take up all the static thingy's. Further since E is a subclass of D it'll first need to load D. So first load D then load E & then you first create D & next you create E. Here's how it'll go -
static initializer of D - 2
static initializer of E - 6
instance initializer of D - 1
creation of D - 3
instance initializer of E - 5
creation of E - this calls E with String args - 8
next line in E's no-args constructor - 7
For your ques 4 -
JLS 8.8.5 Constructor Body -
.........If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
So the D with String args never gets called
HTH
Ashish H.
[ September 10, 2002: Message edited by: Ashish Hareet ]
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if I'm getting this right, the order goes like this:
static initializers
class initializers
constructors (super() always called first if this() or super() not called in the first line of the constructor)
correct?
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if I'm getting this right, the order goes like this:
static initializers
class initializers
constructors (super() always called first if this() or super() not called in the first line of the constructor)
correct?

Loosely said , yes.
static initializers won't be called more than once & super classes will always be called before subclasses.
See JLS 12.4 & 12.5
Correct me if I am wrong
Ashish H.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. When classes are loaded: (before objects are created:
1. static initializer of super class (recursive)
2. static initializer of derived class
B. When object is created:
1. constructor code of derived class is
launched:
a. if there is no explicit call for a super
class constructor (or a call to another
constructor of derived class), an implicit
call super()is placed. If there is a call
for super/derived class constructor, it has
to be first statement. This behavior is
repeated in all constructors. So if another
constructor of derived class is called with
this(string s), for example, that constructor
will call super() constructor. So one way or
other, super() gets called.

i. super constructor will call its own
super class constructor (recursive)
ii. instance initializers of super class
iii. code in super class constructor.
c. instance initializers of derived class
b. rest of the code in derived class
constructor.
[ September 10, 2002: Message edited by: Barkat Mardhani ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic