Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

compile error in constructor  RSS feed

 
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
 
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.
 
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();
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!