• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bad constructor?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Must be something very basic, but I can't see why this will not compile. The second attempt will compile, but not print the message in the constructor. What's going wrong?




The errors are:


C:\Documents and Settings\Paul\Desktop\ClassTest>javac MyClass.java
MyClass.java:6: error: constructor MyClass in class MyClass cannot be applied to
given types;
MyClass myClassInstance = new MyClass( i );
^
required: no arguments
found: int
reason: actual and formal argument lists differ in length
1 error


Also, if I modify it as below (remove the constructor argument and add a message to print) then it will compile but not print the message in the constructor when run.

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main problem is that your is not a constructor definition. That is why the compilation error alludes to the "int i" constructor not being defined.

Hint: constructors do not define a return type. Have a look at the Java Tutorials for more info.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Constructors cannot have return type and that is the basic difference between a method and a constructor.

Here compiler is considering void MyClass() as a method and hence a default contructor with no arguments is provided by the compiler for your class.

And when you are passing argument in your new MyClass(i), no match is found for this constructor.

Solution: Do not give any return type keep it "public MyClass( int i )" this will work for you...
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the same reason why when you remove the 'i' you don't get anything to print. with the word "void", it is not a constructor, but a method. So when you call the constructor, you get the default, no-arg constructor that the java compiler gives you for free. remove the word void from the no-arg one, and it will work.
 
Marshal
Posts: 79508
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deep semwal, welcome to the Ranch

I actually think it is a confusing feature that methods with the same identifier as the class are allowed at all. But it's too late to change now.
 
deep semwal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you.. campbell ritchie.

yes it is confusing.. but that is the way it is...
 
Paul Berry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah....no return type. Well that killed an hour this morning trying to figure out.

Thank you all!
 
reply
    Bookmark Topic Watch Topic
  • New Topic