Ask Question

If you declare a variable as an instance variable within a class, and you declare and use the same variable name within a method of the class, then within the method, a) the variable used inside the method takes precedence b) the class instance variable takes precedence c) the two variables refer to a single memory address d) an error will occur

+4
Answers (1)
  1. 15 April, 06:16
    0
    Option (a) is the correct answer for the given question.

    Explanation:

    If we declared a variable with the same name in class and method both then the variable which is declared inside the method overrides the definition of variable which is declared in class. It means the variable which is used inside the method takes precedence.

    For example:

    public class Main

    {

    int c=90; / / class varaible or instance varaible

    void sum1 ()

    {

    int c=4; / / method having same name as class varaible name

    c=c+10;

    System. out. println (c);

    }

    public static void main (String[] args)

    {

    Main ob=new Main ();

    ob. sum1 ();

    }

    }

    Output:

    14

    In this example the variable 'c' is declared in both class and function but variable c takes the precedence inside the method sum1 () and display 14.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “If you declare a variable as an instance variable within a class, and you declare and use the same variable name within a method of the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers