If a local variable has the same name as one of the instance variables in a program it is called as

If a local variable has the same name as one of the instance variables in a program it is called as

The scope of a variable is defined as where a variable is accessible or can be used. The scope is determined by where you declare the variable when you write your programs. When you declare a variable, look for the closest enclosing curly brackets { } – this is its scope.

Java has 3 levels of scope that correspond to different types of variables:

  • Class Level Scope for instance variables inside a class.

  • Method Level Scope for local variables (including parameter variables) inside a method.

  • Block Level Scope for loop variables and other local variables defined inside of blocks of code with { }.

The image below shows these 3 levels of scope.

If a local variable has the same name as one of the instance variables in a program it is called as

Figure 1: Class, Method, and Block Level Scope

If a local variable has the same name as one of the instance variables in a program it is called as
Check Your Understanding

5-8-1: Click on all the variable declarations that are at Class Level Scope.Remember that the instance variables declared at the top of the class have Class Scope.

public class Name {

    private String first;
    public String last;

    public Name(String theFirst, String theLast) {
        String firstName = theFirst;
        first = firstName;
        last = theLast;
     }
}

5-8-2: Click on all the variable declarations that are at Method Level Scope.Remember that the parameter variables and the local variables declared inside a method have Method Level Scope.

public class Name {

    private String first;
    public String last;

    public Name(String theFirst, String theLast) {
        String firstName = theFirst;
        first = firstName;
        last = theLast;
     }
}

Local variables are variables that are declared inside a method, usually at the top of the method. These variables can only be used within the method and do not exist outside of the method. Parameter variables are also considered local variables that only exist for that method. It’s good practice to declare any variables that are used by just one method as local variables in that method.

Instance variables at class scope are shared by all the methods in the class and can be marked as public or private with respect to their access outside of the class. They have Class scope regardless of whether they are public or private.

Another way to look at scope is that a variable’s scope is where it lives and exists. You cannot use the variable in code outside of its scope. The variable does not exist outside of its scope.

If a local variable has the same name as one of the instance variables in a program it is called as
Coding Exercise

Try the following code to see that you cannot access the variables outside of their scope levels in the toString() method. Explain to someone sitting next to you why you can’t access these. Try to fix the errors by either using variables that are in scope or moving the variable declarations so that the variables have larger scope.

If there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable, as seen below. We’ll see in the next lesson, that we can distinguish between the local variable and the instance variable using the keyword this to refer to this object’s instance variables.

In this example, the local variable is used instead of the instance variable of the same name. What will the code print out? Try it with the CodeLens button.

5.8.1. Programming Challenge : Debugging¶

Debug the following program that has scope violations. You may need to add methods or use methods that are in the class Fraction appropriately. Then, add comments that label the variable declarations as class, method, or block scope.

5.8.2. Summary¶

  • Scope is defined as where a variable is accessible or can be used.

  • Local variables can be declared in the body of constructors and methods. These variables may only be used within the constructor or method and cannot be declared to be public or private.

  • When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.

  • Formal parameters and variables declared in a method or constructor can only be used within that method or constructor.

5.8.3. AP Practice¶

    5-8-6: Consider the following class definitions. Which of the following best explains why the class will not compile?

    public class Party
    {
        private int boxesOfFood;
        private int numOfPeople;
    
        public Party(int people, int foodBoxes)
        {
            numOfPeople = people;
            boxesOfFood = foodBoxes;
        }
    
        public void orderMoreFood(int additionalFoodBoxes)
        {
            int updatedAmountOfFood = boxesOfFood + additionalFoodBoxes;
            boxesOfFood = updatedAmountOfFood;
        }
    
        public void eatFoodBoxes(int eatenBoxes)
        {
            boxesOfFood = updatedAmountOfFood - eatenBoxes;
        }
    }
    

  • The class is missing an accessor method.
  • There is a scope violation.
  • The instance variables boxesOfFood and numOfPeople should be designated public instead of private.
  • There is a scope violation. Instance variables are usually private.
  • The return type for the Party constructor is missing.
  • There is a scope violation. Constructors do not have return types.
  • The variable updatedAmountOfFood is not defined in eatFoodBoxes method.
  • There is a scope violation. The updatedAmountOfFood variable is a local variable in another method.
  • The Party class is missing a constructor
  • There is a scope violation.

    5-8-7: Consider the following class definition.

    public class Movie
    {
        private int currentPrice;
        private int movieRating;
    
        public Movie(int p, int r)
        {
            currentPrice = p;
            movieRating = r;
        }
    
        public int getCurrentPrice()
        {
            int currentPrice = 16;
            return currentPrice;
        }
    
        public void printPrice()
        {
            System.out.println(getCurrentPrice());
        }
    }
    

    Which of the following reasons explains why the printPrice method is “broken” and only ever prints out a value of 16?

  • The private variables currentPrice and movieRating are not properly initialized.

  • The constructor will initialize them.

  • The private variables currentPrice and movieRating should have been declared public.

  • Instance variables should be private.

  • The printPrice method should have been declared as private.

  • Methods are usually public.

  • currentPrice is declared as a local variable in the getCurrentPrice method and set to 16, and will be used instead of the instance variable currentPrice.

  • Correct!

  • The currentPrice instance variable does not have a value.

  • Accessor methods are usually public.

You have attempted of activities on this page

In which process a local variable has the same name as one of the instance variables medium?

17) In which process, a local variable has the same name as one of the instance variables? Explanation: There are following reasons for considering a variable shadowing, they are listed below: When we define a variable in a local scope with a variable name same as the name of a variable defined in an instance scope.

Can local variable have same name as instance variable?

If a local variable and an instance variable have the same name, the local variable shadows or hides the name of the instance variable within the scope of the method.

What happens when the parameter has the same name as an instance variable?

There's no problem with giving parameter names and instance variable names the same name. But Java has to pick whether it is an instance variable or a parameter variable. Either way, it doesn't do what you think it should do. It doesn't initialize the instance variables with the values of the parameter variables.

When a local variable and an instance variable have the same name which variable takes precedence in a method?

They're only accessible within the method they are declared inside. If there is a local variable and an instance variable with the same name, then the local variable will take precedence over the instance variable.