+ Start a Discussion
Donnie IsaacDonnie 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! 

 
Best Answer chosen by Donnie Isaac
Neetu_BansalNeetu_Bansal
Hi Donnie,

To access the variable, you need to set the scope as public. Use the below code:
public class myClass
{
   public 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 ();
   }
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Prabhat Kumar12Prabhat Kumar12
Its working fine in my ORG. Please logout your account and try agarin.
Donnie IsaacDonnie Isaac
Hey Prabhat,
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. 
Neetu_BansalNeetu_Bansal
Hi Donnie,

To access the variable, you need to set the scope as public. Use the below code:
public class myClass
{
   public 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 ();
   }
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
Raja236Raja236
even i can able to get the result
Integer a = myClass.calculate();
system.debug('result'+a);

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
Donnie IsaacDonnie Isaac
Thanks everyone as all of you helped! Neetu - thanks for helping me fix the scope.