Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+9 votes
asked in CSC211_Winter2018 by (8 points)

2 Answers

+2 votes

because the variable is not in the scope of the different static method.

answered by (8 points)
+1 vote

This is due to the scope of the variable! (Check out page 99-100 for more on scope)
Scope restricts where a variable can and cannot be used. Methods are like containers holding objects (variables). Because of this the variable can only be used within its container (the scope, in this case the method) that it was declared in.

This can also cause issues when you declare a variable within a loop- the variable only exists within the loop, and therefore can not be used or called outside of said loop.

To use a variable across methods it’s important to create a class constant, as described on page 109.

public static final <type> <name> = <expression>; 

For example, this is the format for adding a constant which could be called in multiple methods.

answered by (8 points)
...