Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

Java Error: The constructor is undefined

In Java, Why am I getting this error:

Error: The constructor WeightIn() is undefined

Java Code:

public class WeightIn{
  private double weight;
  private double height;

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn;
    height = heightIn;
  }
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

public class WeightInApp{
  public static void main (String [] args){
    WeightIn weight1 = new WeightIn();         //Error happens here.
    weight1.setWeight(3.65);
    weight2.setHeight(1.7);
  }
}

I have a constructor defined.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this to your class:

public WeightIn(){
}
  • Please understand that default no-argument constructor is provided only if no other constructor is written
  • If you write any constructor, then compiler does not provided default no-arg constructor. You have to specify one.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...