Java Question

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Another one of my famous quick questions (Java noob):

I have an error:
http://i33.tinypic.com/o7368g.png
and a source:
http://pastebin.com/d732cdaba
when compiled produces:
http://pastebin.com/m7ea0a6ba

Whats wrong?

Problem semi-solved:

Netbeans didn't recognize the int constructor, what is the difference between these two constructors, or is Netbeans just having a fit?:
Code:
Circle(int radius) {
        this.radius = radius;
    }
Code:
public void Circle(int radius) {
        this.radius = radius;
    }
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Code:
public void Circle(int radius) {
        this.radius = radius;
    }

This isn't a constructor, it's a method named Circle that takes an int and doesn't return anything.
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
Another one of my famous quick questions (Java noob):

I have an error:
http://i33.tinypic.com/o7368g.png
and a source:
http://pastebin.com/d732cdaba
when compiled produces:
http://pastebin.com/m7ea0a6ba

Whats wrong?

Problem semi-solved:

Netbeans didn't recognize the int constructor, what is the difference between these two constructors, or is Netbeans just having a fit?:
Code:
Circle(int radius) {
        this.radius = radius;
    }
Code:
public void Circle(int radius) {
        this.radius = radius;
    }


Constructors shouldn't have return type. Just remove the void in front of Circle(int radius) method.

and run it. :)
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
This isn't a constructor, it's a method named Circle that takes an int and doesn't return anything.

In theory, but the classname is "Circle" too, so the method's name is illegal. Besides that, method names should never start with a capital.

Constructors are usually defined as
Code:
public <classname>(<arguments) {
    <body>
}
And as gsonline said, that's equivalent to removing the "void" part of the method signature.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Well, it is more of a logical question. I already know how to fix the problem, I just want to know why. If a constructor doesn't return anything, then why is putting a void return type wrong (like how specifying that a method is public and specifying engender the same result)?
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
The constructor is for initializing the values while creating the object of the class. The java compiler distinguishes the constructor by its name same as classname and no retun type. There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. If it has return type then compiler takes it as method rather than constructor

Hope this helps...
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Oooo, I confuse no return type with no return value. Thanks for your help :)
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
In theory, but the classname is "Circle" too, so the method's name is illegal. Besides that, method names should never start with a capital.
Legal, but discouraged. From section 8.4 "Method Declarations" of the Java Language Specification, Third Edition:
A class can declare a method with the same name as the class or a field, member class or member interface of the class, but this is discouraged as a matter of syle.
 
Top