Using void with constructors that return no value
Hello everyone,
I am new to Java and new to forums as well.
My questions is: Why can't a constructor that accepts two int values BUT RETURNS NO VALUES have the "void" modifier?
It took me 2 days to find out that's why the program wouldn't compile: Code:
public [COLOR="Red"]void[/COLOR] Rectangle (int w, int h) {
if (w > 0 && w < 30 && h > 0 && h < 30) {
width=w;
height=h;
System.out.println("A rectangle was created with width="+width+"and height="+height); }
else{ System.out.println("Width must be from 1-29");
}
}
This will NOT compile, but without the "void", it will. "cannot find symbol", it said. It puzzled me for 2 horrible days because since it does not return a value, it should accept it. I didn't need it to be void but still it was very hard to figure out the problem. Does anyone know why it won't accept it? I'm fine now, but I want to learn why it thins it's returning a value, is it the println?
Thanks!
*Edited to add "[/code]"