Issue
I am trying to execute the below code. My IDE tells me
USER_ROLE_EMPLOYEE cannot be resolved to a variable
Here is my code:
public class TestUserHelper {
@Before
public void setUp() throws Exception {
String USER_ROLE_EMPLOYEE="Service Desk";
String USER_ROLE_MANAGER="Store Manager";
}
@Test
public void testIsUserRoleValidEmployee() {
Assert.assertTrue(UserValidator.validateSupervisor(USER_ROLE_EMPLOYEE));
}
@Test
public void testIsUserRoleValidSupervisor() {
Assert.assertTrue(UserValidator.validateSupervisor(USER_ROLE_MANAGER));
}
}
I am getting an error saying USER_ROLE_EMPLOYEE
, USER_ROLE_MANAGER
cannot be resolved to a variable.
What am I doing wrong?
Solution
Java has different types of variables:
- There are local variables. These are declared inside of a method and can only be accessed from within that same method. We say these variables have local scope. Your
USER_ROLE_EMPLOYEE
andUSER_ROLE_MANAGER
variables are local variables. Thus, accessing them from a method other than the one they were declared in is not possible. - There are instance variables. These exist separately within each instance of your class and can be accessed from any non-static method inside your class. As opposed to local variables, they may also be accessible from outside your class; however an instance of the class is always required. You declare them just like local variables, except that you put the declaration outside of any method (directly in your class). They should go at the top. You should add an access modifier (like
private
) to them, read more about that here. - And then there are class variables (
static
variables). Unlike instance variables, these exist just once and are accessible from any method within your class (evenstatic
ones). Declare class variables in the same place as instance variables, but using thestatic
keyword. Like instance variables, these will be accessible from outside of your class depending on the access modifier you declared. Unlike instance variables, they will not require an instance of your class.
So, in your code you can:
Use instance variables instead of local variables:
public class TestUserHelper { private String USER_ROLE_EMPLOYEE, USER_ROLE_MANAGER; @Before public void setUp() throws Exception { USER_ROLE_EMPLOYEE = "Service Desk"; USER_ROLE_MANAGER = "Store Manager"; } @Test public void testIsUserRoleValidEmployee() { Assert.assertTrue(UserValidator.validateSupervisor(USER_ROLE_EMPLOYEE)); } @Test public void testIsUserRoleValidSupervisor() { Assert.assertTrue(UserValidator.validateSupervisor(USER_ROLE_MANAGER)); } }
Or, and I would recommend this second approach, as your variables are really constants: Declare them as
static
(class variables) andfinal
(constants). You will not be able to initialize them insetUp()
any more, but seeing as they are constant, that is not a problem:public class TestUserHelper { private static final String USER_ROLE_EMPLOYEE = "Service Desk"; private static final String USER_ROLE_MANAGER = "Store Manager"; @Test public void testIsUserRoleValidEmployee() { Assert.assertTrue(UserValidator.validateSupervisor(USER_ROLE_EMPLOYEE)); } @Test public void testIsUserRoleValidSupervisor() { Assert.assertTrue(UserValidator.validateSupervisor(USER_ROLE_MANAGER)); } }
Answered By - O.O.Balance