Results 1 to 13 of 13
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default 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
    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.
    My question:
    Is quote's aim above program?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    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

    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.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Ah alright. So the first thing that occurs is:

    Java Code:
    		this(1);
    		this(2,5);
    Next
    Java Code:
    		this.a=a;
    		this.b=b;
    then

    Java Code:
    		System.out.println("a= "+a);//a=1
    		System.out.println("b= "+b);//b=5
    Finally

    Java Code:
    		this.a=a;
    Correct?
    "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)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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() <--

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Thank you.
    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.
    The following program is good for your quote's aim?

    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
    	}
    }

  9. #9
    gcampton Guest

  10. #10
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    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.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcampton View Post
    True, the underscores in the name Row_OF_This are a bit funny but all of this has nothing to do with the problem being discussed.

    Jos

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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();
        }
    }

  13. #13
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •