0

I'm new to Java. There's a question on the my recent Java test which I don't really understand, and hope that you guys will help me with it.

Analyze the following code:

class Circle {
    private double radius;
    public Circle(double radius) {
        radius = radius;
    }
}

Here are the answers:

A) The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

B) The program has a compilation error because you cannot assign radius to radius.

C) The program has a compilation error because it does not have a main method.

D) The program does not compile because Circle does not have a default constructor.

In my opinion, I think A is correct. B is clearly wrong because you can of course do the assignment. C is wrong because not having the main method is completely fine. D is also wrong because default constructor is not necessary. This left me with A.

I don't understand the wording of A. I think this code can be changed to:

this.radius = radius;

to be correct.

Can anyone help me clarify this? Thank you a lot!

1
  • 1
    Yes you are right, the program can be changed as you specify to work correctly. But A does not address correcting the code, and yes you are right that answer A is correct. Commented Dec 11, 2016 at 23:14

3 Answers 3

1

Literaly, A is correct.

The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

You can still create an Object of Circle, but its radius is always 0. meaning, you assign a wrong declaration.

In this Constructor:

public Circle(double radius) {
    radius = radius; 
}

You only assign your radius parameter to itself, meaning you didn't actually pass it to the class variable radius. so that's why you use this to pertain a class member.

so the right declaration would be passing the radius parameter variable to radius class variable.

this.radius = radius;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help!
0

When you are in a constructor where you have arguments with the same name as class variables, you reference the argument with just the name of the variable (in your case radius) and the class variable with this.radius. Therefore the line just sets the value of the argument radius to itself, effectively doing nothing. Therefore the class variable radius is unchanged from it's default value of 0.0, meaning A is indeed correct. To correct the code such that the constructor sets the radius, it should be changed to this.radius = radius; as you stated in the question.

Comments

0

Yes you right about your answer. The above code will compile with no errors but an instance of Circle object will not be created with a specified radius because the argument double radius is saved locally and the class has not been initiated.

You can save argument variables locally or save it in the parent by using this.radius.

You only need main to run the program.

You loose the default constructor once you create one that takes an argument and need to create the default one but in this case it is not needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.