You need to sign in to do that
Don't have an account?
Donnie Isaac
Apex Question: Variable not visible
Hi all. Learning Apex here. Refere to code and question below:
Code:
public class myClass {
static final Integer PRIVATE_INT_CONST;
static final Integer PRIVATE_INT_CONST2 = 200;
public static Integer calculate () {
return 2 + PRIVATE_INT_CONST2;
}
static {
PRIVATE_INT_CONST = calculate ();
}
}
Trying to get this code [ see code below] to (run in Execute Anonymous and ) return what I think should be '202'. At first I tried to run this code and added System.debug (PRIVATE_INT_CONST); and the error was that only top level class variables can be declared static. [ What is a top level class? Is this just an outer classs?] So I created the class as a new class. Now that the class is created, it seems that it is reconizing it. So I simply entered System.debug(myClass.PRIVATE_INT_CONST); in the Execute Anonymous window and the error is that the variable is not visible. Can anyone help me correct the code so that it will run? What am I doing wrong? Thanks!
Code:
public class myClass {
static final Integer PRIVATE_INT_CONST;
static final Integer PRIVATE_INT_CONST2 = 200;
public static Integer calculate () {
return 2 + PRIVATE_INT_CONST2;
}
static {
PRIVATE_INT_CONST = calculate ();
}
}
Trying to get this code [ see code below] to (run in Execute Anonymous and ) return what I think should be '202'. At first I tried to run this code and added System.debug (PRIVATE_INT_CONST); and the error was that only top level class variables can be declared static. [ What is a top level class? Is this just an outer classs?] So I created the class as a new class. Now that the class is created, it seems that it is reconizing it. So I simply entered System.debug(myClass.PRIVATE_INT_CONST); in the Execute Anonymous window and the error is that the variable is not visible. Can anyone help me correct the code so that it will run? What am I doing wrong? Thanks!
To access the variable, you need to set the scope as public. Use the below code: Let me know, if you need any other help.
Thanks,
Neetu
All Answers
I logged out and back in (to a sandbox) and still I am getting the error. [I made sure that we were working "online" and that "build automatically" is checked, so no problem there. I even changed some things, savedin the Force.com IDE, and checked the org and the saves were there. ] If I pull System.debug(calculate()); it returns 202 as expected. Why can't I get it to return that with System.debug(myClass.PRIVATE_INT_CONST); ?
Still says variable is not visible. If this is a scope problem, how do I fix it? Thanks for your help.
To access the variable, you need to set the scope as public. Use the below code: Let me know, if you need any other help.
Thanks,
Neetu
i hope you are aware of class execution process
first it initilize static intilizer, in this your class it shoul be null
next constructor and next methods