Results 1 to 13 of 13
- 01-19-2010, 09:30 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
having several this() invocations in a row
Java Code:class A { private int a; private int b; A() { this(1); System.out.println("a= "+a);//a=1 System.out.println("b= "+b);//b=5 } A(int a) { this(2,5); this.a=a; } A(int a, int b) { this.a=a; this.b=b; } } public class Row_OF_This { public static void main(String args[]) { A ob=new A(); } }
Hello
My question:From Object Initialization in Java
You can have several this() invocations in a row if you wish. In other words, you could have an <init> method that invokes another with this(), and that <init> method invokes yet another with this(), and so on.
Is quote's aim above program?
- 01-19-2010, 09:47 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Your program seems to be a good example of what the quote is saying.
The important point being made comes just after the bit you quoted: "But in the end, there will always be an <init> method with a super() invocation -- either an explicit super() invocation or a compiler-generated one. Since this() and super() are both always the first action a constructor takes, the instance variables will always be initialized in order from the base class on down."
To show this perhaps you could have A extends some other class B. And have the A(int,int) constructor call super(). The aim is to see the order in which things happen - particularly the "base first" bit - so perhaps all of the constructors could have System.out.println() statements to illustrate this.
- 01-19-2010, 10:51 PM #3
I got a question. This will never occur right?
Java Code:System.out.println("a= "+a);//a=1 System.out.println("b= "+b);//b=5"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 01-19-2010, 11:06 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
What I see when I run the program is
Java Code:a= 1 b= 5
Each of the this() statements causes another constructor to be called. But after that constructor has finished the rest of the first constructor gets to finish.
There are two points here: first, no, the System.out.println()'s still get carried out. And secondly they happen after the constructors later in the chain. The author's point in the article was that this means that the base class constructor will be the first thing to happen and then the class's constructor. If there are a number of constructors chained together they will all get carried out from the end of the chain back to the start.
- 01-19-2010, 11:28 PM #5
Ah alright. So the first thing that occurs is:
NextJava Code:this(1); this(2,5);
thenJava Code:this.a=a; this.b=b;
FinallyJava Code:System.out.println("a= "+a);//a=1 System.out.println("b= "+b);//b=5
Correct?Java Code:this.a=a;
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 01-20-2010, 01:41 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I think you may have in the wrong place.
The way to check - as I said earlier - is to remove the assignments that are going on (for no particular reason). Instead give each of the constructors a single System.out.println() identifying which constructor it is.
By running the code you will see in which order the code following each of the this(...) statements is performed.
There is nothing special about constructors here. Exactly the same thing happens if you have a bunch of methods each one calling the next as its first statement.
- 01-20-2010, 09:28 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The order is like:
Java Code:A() A(int a) A(int a, int b) this(1) --> this(2,5) ---> this.a = a; this.b = b; this.a = a <--- println() <--
- 01-20-2010, 11:44 AM #8
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Hello
Thank you.
The following program is good for your quote's aim?To show this perhaps you could have A extends some other class B. And have the A(int,int) constructor call super(). The aim is to see the order in which things happen - particularly the "base first" bit - so perhaps all of the constructors could have System.out.println() statements to illustrate this.
Java Code:class A { private int a; private int b; A() { System.out.println("a= "+a);//a=0 System.out.println("b= "+b);//b=0 } A(int a) { this(); this.a=a; System.out.println("a= "+a);//a=5 } A(int a, int b) { this(a); this.b=b; System.out.println("b= "+b);//b=6 } int addab() { return a+b; } } class B extends A { B(int a, int b) { super(a,b); } } public class Row_OF_This { public static void main(String args[]) { B ob=new B(5,6); System.out.println("a+b= "+ob.addab());//a+b=11 } }
- 01-20-2010, 11:48 AM #9gcampton Guest
- 01-20-2010, 04:54 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Hello
My aim was not this link. I want to khow that following quote Comeoffs with my second program.
To show this perhaps you could have A extends some other class B. And have the A(int,int) constructor call super(). The aim is to see the order in which things happen - particularly the "base first" bit - so perhaps all of the constructors could have System.out.println() statements to illustrate this.
- 01-20-2010, 05:28 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-20-2010, 06:19 PM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I was thinking more like:
Java Code:class A extends B { A() { this(1); System.out.println("no arg constructor"); } A(int a) { this(1, 2); System.out.println("one arg constructor"); } A(int a, int b) { super(); // not really needed System.out.println("two arg constructor"); } } class B { B() { System.out.println("Base constructor"); } } public class Test { public static void main(String[] args) { new A(); } }
- 01-20-2010, 08:19 PM #13
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks