3

In the code below:

public class Square 
{
    private double side;

    public Square(double a) 
    {
        double side = a;
    }

    public double area()
    {
        return side * side;
    }

    public static void main(String[] args) 
    {
        Square r = new Square(10.0);
        System.out.println(r.area());
    }
}

When the field is declared:

 private double side;

and then in the constructor:

double side = a;

Why is an error not thrown?

I would think so because we are declaring the datatype of the variable double twice. Why does this code execute?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Bob Smith
  • 75
  • 9
  • 1
    This is called variable shadowing: see https://stackoverflow.com/questions/1092099/what-is-variable-shadowing-used-for-in-a-java-class – OH GOD SPIDERS Jan 30 '20 at 14:56
  • 1
    Because however unfortunate that may be, you're allowed to declare new variables of the same name as existing variables, as long as there is no scope clash. Which there isn't there. You are not declaring the datatype of the variable twice. You are declaring two different variables. Also, that would be a compile error, not a runtime error. – kumesana Jan 30 '20 at 14:56
  • 1
    The same reason why it would be legal to have a variable called `side` as a parameter in your constructor and mutators. – Anil Jan 30 '20 at 15:03
  • You aren't declaring the datatype of a variable twice, you are declaring a field and a local variable, those are two different things. – Mark Rotteveel Jan 30 '20 at 15:21

2 Answers2

5

Because the second one declares a variable local to the constructor. It overshadows the member variable (which you can still access there via this.side).

In that context, that declaration is perfectly useless, as that variable will disappear as soon as the constructor ends.

Also, even if it was a problem, it wouldn't throw a runtime error. It would be a compile-time error.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
2

Declaring a variable involves declaring its data type as well, so you are essentially asking about declaring the same variable twice.

You are right in assuming that declaring the same variable twice is not possible, but that's not happening in your code. Your

private double side;

is a data member of Square object, that is, a property of squares. It is declared in the block of the class definition, hence it's a data member.

Your

double side = a;

is a local variable defined in a method, which is the constructor in our case. It's legal syntactically, since it's not a member of Square and hence it has nothing to do with the member. Now, having said that I must mention that you have made a mistake, since inside the constructor you have declared a variable with the same name as your data member, initialized your local variable and then you never use it, while in another method you assume that side is initialized. So you have almost certainly intended to initialize your data member in your constructor, like:

public Square(double a) 
{
    this.side = a;
}

or even

public Square(double a) 
{
    side = a;
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175