File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Bad constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bad constructor?" Watch "Bad constructor?" New topic
Author

Bad constructor?

Paul Berry
Greenhorn

Joined: Apr 07, 2011
Posts: 15
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.



The Hanging Kludge
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
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

Joined: Sep 02, 2011
Posts: 2

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

Joined: Oct 02, 2003
Posts: 11230
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Sep 02, 2011
Posts: 2
thank you.. campbell ritchie.

yes it is confusing.. but that is the way it is...
Paul Berry
Greenhorn

Joined: Apr 07, 2011
Posts: 15
Oh yeah....no return type. Well that killed an hour this morning trying to figure out.

Thank you all!
 
wood burning stoves
 
subject: Bad constructor?