• Post Reply Bookmark Topic Watch Topic
  • New Topic

compile error in constructor  RSS feed

 
Apoorv Kodera
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is the class i wrote below, but when i compile i get compilation error why is this..?

class ch0401{

ch0401(){
System.out.println("message");
}

ch0401(String s){
System.out.println(s);
}

ch0401(int i){
ch0401();
ch0401("overloaded");
}

public static void main(String[] s){
ch0401 obj1 = new ch0401();
ch0401 obj2 = new ch0401("overloaded");
ch0401 obj3 = new ch0401(1);
}
}

ch0401.java:12: cannot find symbol
symbol : method ch0401()
location: class ch0401
ch0401();
^
ch0401.java:13: cannot find symbol
symbol : method ch0401(java.lang.String)
location: class ch0401
ch0401("overloaded");
^
2 errors
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Apoorv Kodera

The error is in the below constructor.



When you need to call a constructor in the same class from another constructor, you should use this(<arguments>) and not give the constructor name. Also note that there can be only one call to any other constructor and hence only one this is allowed as the first line of the constructor.

As a side note you use super() to call a constructor in the super class. Read more on constructors here.

Please UseCodeTags henceforth.
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is like that
class ch0401{
ch0401(){
System.out.println("message");
}

ch0401(String s){
System.out.println(s);
}
ch0401(int i) {
//super();
//ch0401("overloading");
}
public static void main(String[] args){
ch0401 obj1 = new ch0401();
ch0401 obj2 = new ch0401();
ch0401 obj3 = new ch0401();
}
}
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!