简体   繁体   中英

How do i pass a boolean into a new object?

public class Clown {
    boolean standing = false;

    public Clown(boolean standing) { 
        Clown clown = new Clown(standing);
    }

I want to make a new Clown object with the boolean passed in but everytime I run this I get a stack overflow error. How can I fix this?

You obtain a StackOverflow Exception because you are doing a "recursive" call when you invoke the constructor from itself.

Do:

public class Clown {

  boolean standing;

  public Clown(boolean standing) {
      this.standing  = standing;
  }

Then create the Object from some method of some class with:

Clown clown = new Clown(standing);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM