Ask Question
28 November, 05:46

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. 28 November, 08:11
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers