File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bad constructor?

 
Paul Berry
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Beckett
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
deep semwal
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah....no return type. Well that killed an hour this morning trying to figure out.

Thank you all!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic