Ask Question
1 August, 20:04

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

+5
Answers (1)
  1. 1 August, 20:41
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers