Question: Run the ScopeOfVariables.java program and report the result. Then remove the comments in line 6, 7, 9, 10, 15 and 16 to check the value
Run the ScopeOfVariables.java program and report the result. Then remove the comments in line 6, 7, 9, 10, 15 and 16 to check the value of x in different methods. Report the result.
-
What is the value of x after calling update method? Explain why.
-
modify update method so that it returns updated value of x and you see the
changes on x.
-
Comment out line 14 and remove the comment of line 4 and run the program.
What happens? Explain why.
-
Comment out line 4 and remove the comment of line 2 and run the program.
What is the value of x after calling update method? Explain why.
-
Comment out line 2 and line 14 and run the program. What is the value of x
after calling update method? Explain why.
-
Compare the modifiers in line 2 and line 14.
-
Remove static modifier in line 2 and compile and run the code. What happens and why?
code:
public class ScopeOfVariables { //final static int ADD = 15; public static void main(String []args){ //final int ADD = 5; double x = 10; //System.out.print("In main before updating - "); //System.out.println("x = " + x ) ; update(x); //System.out.print("In main after updating - "); //System.out.println("x = " + x) ; } public static void update (double x){ final int ADD = 5; //System.out.print("In update before updating - "); //System.out.println("x = " + x ) ; x += ADD; System.out.print("In update after updating - "); System.out.println("x = " + x ) ; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
