0

I'm a student. Just got back a homework assignment stating I should call the constructor method rather than reusing the same code. I copied the code because I could not call the constructor without an error. What is the proper syntax to call the constructor method...from a separate method?

I did do a search, but I could not find this specific (within the class) issue. I did try using "this" as well as creating a class instance too, but I keep getting errors.

import java.util.Random;
public class Coin {

    // variable for a generic coin toss
    private String sideUp;  

    // Constructor 

    // ******************** Instructor notes...
    // This is the same code as your toss() method
    // It is OK to call that method from your constructor.
    // Don't copy/paste code or repeat yourself if not required.
    public Coin() { 
        Random rand1 = new Random();
        int x = rand1.nextInt(2);   
        if (x > 0){
            sideUp = "Heads";
        }else{
            sideUp = "Tails";
        }
    }


    //Void Method
    public void toss() {
        // how to call the Coin constructor above??????????????????????????
        Coin();
    } 
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user3352523
  • 1
  • 1
  • 3
  • Coin newCoin = new Coin(); – MightySchmoePong Feb 27 '14 at 19:19
  • 2
    Do it the other way around. Move the code back to your toss method and just call `toss()` from inside the constructor. – Mike B Feb 27 '14 at 19:19
  • @MikeB Bad idea. You shouldn't call that method from the constructor. See my answer for why. – Rohit Jain Feb 27 '14 at 19:26
  • @RohitJain So make the method final. – Mike B Feb 27 '14 at 19:39
  • @user3352523 Calling `new Coin()` worked?? Are you sure? I'm sure it compiled, but did it really do the job? That will create a **new** `Coin` object, and it will set the `sideUp` field of that new object, but it won't set the `sideUp` field of the `Coin` you want it to. – ajb Feb 27 '14 at 20:26

3 Answers3

2

Do it the other way around. Move the code back to your toss method and just call toss() from inside the constructor.

import java.util.Random;
public class Coin {

   // variable for a generic coin toss
   private String sideUp;  

   // Constructor 

   // ******************** Instructor notes...
   // This is the same code as your toss() method
   // It is OK to call that method from your constructor.
   // Don't copy/paste code or repeat yourself if not required.
   public Coin() { 
       toss();  
   }


   //Void Method
   public final void toss() {
       Random rand1 = new Random();
       int x = rand1.nextInt(2);   
       if (x > 0){
           sideUp = "Heads";
       }else{
           sideUp = "Tails";
       }
   } 
}

As is pointed out in other comments and answers, calling methods that may be overriden from a constructor is a bad idea. Here's a good explanation for why that is: Why is it considered bad practice to call a method from within a constructor?

You can make the method final as I did here to avoid the problems.

Community
  • 1
  • 1
Mike B
  • 5,390
  • 2
  • 23
  • 45
1

To use a constructor you need to use the 'new' keyword. e.g.

Coin myCoin = new Coin();
Richard Miskin
  • 1,260
  • 7
  • 12
  • This will not solve his problem. He wants to reuse code, not create a new object. – ajb Feb 27 '14 at 19:34
  • It does answer the stated question - "How to call a constructor within the class in another method". – Richard Miskin Feb 27 '14 at 19:36
  • Not really. The rest of the question asks "how to call a constructor method"---and since there's really no such thing as a constructor method, it's necessary to look harder at the question to find out what the questioner is actually asking. We can't always expect new learners to use terminology correctly. – ajb Feb 27 '14 at 19:58
1
// ******************** Instructor notes...
// This is the same code as your toss() method
// It is OK to call that method from your constructor.

I'm afraid the 3rd statement is not really true. It is actually not ok to call an overridable method in your constructor. That will leak the this reference before the object has been fully initialized. That could give you unexpected result, if you override your method in subclass. You should confirm this with your instructor.

BTW, the instructor doesn't says to call constructor from method, but the other way round. But you wouldn't do either of them. Just move the code from constructor to the toss() method, if that part of code has to be the part of toss() method.

Or if you really want those codes to be executed both in constructor and toss() method, then create a private method in your class, move those codes there, and call it from both the places:

public class Coin {

    // variable for a generic coin toss
    private String sideUp;  

    public Coin() {
        initSideUp();
    }

    //Void Method
    public void toss() {
        initSideUp();
    }

    private void initSideUp() { 
        Random rand1 = new Random();
        int x = rand1.nextInt(2);   
        if (x > 0){
            sideUp = "Heads";
        }else{
            sideUp = "Tails";
        }
    }

}
Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Excellent point. He should declare a `private` method to do it, and then have both the constructor and the public `toss` call that. – ajb Feb 27 '14 at 19:31