This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
MyClass myClassInstance = new MyClass( i );
required: no arguments
reason: actual and formal argument lists differ in length
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.
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