# Variables with the same name...

• Introduction

• You have seen the scoping rules of 3 different categories of variables in: click here

• Local variables
• Parameter variables
• Instance variables

• You have learned to determine the region where a veriable is accessible.

• There are a few more scoping rules determining:

 What happens when we define two different variables but with the same name in the same scope (region)

• Variables with the same name in non-overlapping scopes

• Clearly, because the scopes do not overlap, there is no problem in using the same name again.

• Example 1:

 ``` public class RectangleTester { public static double area(Rectangle rect) { // <---- Start block 1 double r = rect.getWidth() * rect.getHeight(); // (1) return r; } // <---- End block 1 public static void main(String[] args) { // <---- Start block 2 Rectangle r = new Rectangle(5, 10, 20, 30); //(2) double a = area(r); System.out.println(r); }// <---- End block 2 } ```

• There are 2 different variables with name r:

```	double r;     (1)

Rectabgle r;  (2)
```

• The scope of double r is "block 1"

• The scope of Rectangle r is "block 2"

• These 2 blocks do not overlap. And therefore, there is no ambiguity possible when the variable r is used.

(Ambiguity can only occur when both variables are accessible in the same region of the program - for this to happen, the scopes must overlap).

• Example 2:

 ``` public class Scoping1 { public static void main(String[] args) { int i = ...; if ( i > 15 ) { // Start of Block 1 int j = 20; // (1) System.out.println(j); // (A) } // End of Block 1 else { // Start of Block 2 String j = "Hello"; // (2) System.out.println(j); // (B) } // End of Block 2 } } ```

• There are 2 different variables with name j:

```	int j;        (1)

String j;     (2)
```

• The scope of int j is "block 1"

• The scope of String j is "block 2"

• These 2 blocks do not overlap. And therefore, there is no ambiguity possible when the variable j is used:

• The use of variable j on line (A) is inside the scope of int j and will therefore refer to this variable.

• The use of variable j on line (B) is inside the scope of String j and will therefore refer to this variable.

• Multiple local/parameter variables of the same name in overlapping scopes

• NOTE: A parameter variable behaves the same way as a local parameter in term of scope.

What is true for a local variable is also true for a parameter variable when applying scoping rules...

• In Java, you CANNOT have 2 different local/parameter variables with the same name in the same (overlapping) scope

• Example:

 ``` public class Scoping2 { public static void main(String[] args) { // Start of Block 1 int i = ...; double j = 40; // (1) if ( i > 15 ) { // Start of Block 2 int j = 20; // (2) System.out.println(j); } // End of Block 2 else { // Start of Block 3 String j = "Hello"; // (3) System.out.println(j); } // End of Block 3 } } // End of Block 1 ```

• A variable double j is defined inside block (1)

• A variable int j is defined inside block (2)

• A variable String j is defined inside block (3)

• Block (2) and (3) are contained inside block (1)

• Inside block (2), two different variables j are potentially accessible:

• The variable double j which was defined inside block (1)
• The variable int j which was defined inside block (2)

• Inside block (3), two different variables j are potentially accessible:

• The variable double j which was defined inside block (1)
• The variable String j which was defined inside block (3)

• Jave will report error when two different local variables are accessible in some scope (some region of the program).

When you compile the program above, you will get this error:

```Scoping2.java:11: j is already defined in main(java.lang.String[])
int j = 20;
^
Scoping2.java:17: j is already defined in main(java.lang.String[])
String j = "Hello";
^
```

The error message "j is already defined" means that there is already a local variable with the name j defined and you can't use the name j again....

To correct this error, use a different name (that is if you really want to define a new variable, maybe you don't want the variable, in that case, delete the second definition)...

• Example Program: (Demo above code)

Compile the program to see the error message...

• Multiple instance variables of the same name in a class

• In Java, you CANNOT have 2 different instance variables with the same name in the same class

• Example:

 ``` public class BankAccount { ... public void deposit(double amount) { balance = balance + amount; } private double balance; private int balance; // ??? } ```

• A variable double balance is defined inside the class

• A variable int balance is now also defined inside the class

• Jave will report error when two different instance variables are defined in the same class.

When you compile the program above, you will get this error:

```BankAccount.java:34: balance is already defined in BankAccount
private int balance;      // ?????
^
```

The error message "balance is already defined" means that there is already an instance variable with the name balance defined and you can't use the name balance again....

To correct this error, use a different name (that is if you really want to define a new variable, maybe you don't want the variable, in that case, delete the second definition)...

• Example Program: (Demo above code)

Compile the program to see the error message...

• An instance variable and a local (or parameter) variable with the same name in the same scope

• In Java, you are allowed to have an instance variable, and a local or parameter variable with the same name inside the same scope

• Example:

 ``` public class Car { public Car(double efficiency, double capacity) { efficiency = efficiency; // ???? capacity = capacity; // ???? currGasLevel = 0; odometer = 0; } private double efficiency; private double capacity; private double currGasLevel; private double odometer; } ```

• The instance variables double efficiency and double capacity are defined inside the class

• The parameter variables also have the names: efficiency and capacity

• The scope of the instance variables efficiency and capacity is everywhere in the class - that includes the region inside the constructor method Car()

• The scope of the parameter variables efficiency and capacity is the entire constructor method Car()

• So inside the constructor method Car(), there are 2 sets of different variables with names efficiency and capacity...

• Java will use parameter variables with the unqualified names

In other words:

• efficiency will refer to the parameter variable efficiency
• capacity will refer to the parameter variable capacity

• If there is name conflict between an instance and a local/parameter variable, and you need to refer to the instance variable, you can do so by qualifying the instance variable with the implicit parameter this

In other words:

• this.efficiency will refer to the instance variable efficiency
• this.capacity will refer to the instance variable capacity

• Example:

```
public class Car
{
public Car(double efficiency, double capacity)
{
this.efficiency = efficiency;    // Correct way
this.capacity = capacity;        // Correct way
currGasLevel = 0;
odometer = 0;
}

private double efficiency;
private double capacity;
private double currGasLevel;
private double odometer;
}
```

• Example Program: (Demo above code)

• Self Check

• What is printed by this program:

```
public class Test
{
public static void main(String[] args)
{
x = 17;

{
int x;

x = -5;
}

System.out.println(x);
}

private static int x = 4;
}
```