Wednesday, 29 August 2012

Static Variables, Constants, and Methods - Apex

The variables and methods you've created so far are instance methods and variables, which means you have to first create an instance of the class to use themodelNumber and numberInStock variables. Each individual instance has its own copy of instance variables, and the instance methods can access these variables. There are times when you need to have a member variable whose value is available to all instances, for example, a stock threshold variable whose value is shared with all instances of the Fridge class, and any update made by one instance will be visible to all other instances. This is when you need to create a static variable. Static variables are associated with the class and not the instance and you can access them without instantiating the class.
You can also define static methods which are associated with the class, not the instance. Typically, utility methods that don’t depend on the state of an instance are good candidates for being static. 
  1. Modify the Fridge class by adding the following static variable:
    public static Integer stockThreshold = 5;
  2. Execute the following in the Developer Console:
    System.debug ( Fridge.stockThreshold );
    This will output 5. Note how you didn't have to create an instance of the Fridge class using the new operator. You just accessed the variable on the class.
  3. You can also change the value of this static variable by accessing it through the class name.
    // Modify the static stock threshold value 
        
    Fridge.stockThreshold = 4;
    System.debug ( Fridge.stockThreshold );
    
    This will write 4 to the output.
  4. Sometimes you want to declare a variable as being a constant—something that won't change. You can use the final keyword to do this in Apex; it indicates that the variable might be assigned to a value no more than once. Modify the static variable you just declared to as follows:
    public static final Integer STOCK_THRESHOLD = 5;
    You can still output the value of the field, for example, Fridge.STOCK_THRESHOLD; will work, but you can now not assign any other value to the field, for example,Fridge.STOCK_THRESHOLD = 3; won't work.
  5. Let's define a static class method that prints out the values of a given object that gets passed in as an argument. This will be a great help for debugging. Add a new method to the Fridge class:
    public static void toDebug(Fridge aFridge) {
        System.debug ('ModelNumber = ' + aFridge.modelNumber);
        System.debug ('Number in Stock = ' + aFridge.numberInStock);
    }
  6. Test out this new method by calling it in the Developer Console and passing in a Fridge instance:
    Fridge myFridge = new Fridge('MX-Y', 200);
    Fridge.toDebug(myFridge);
    This is the output you’ll get in the Developer Console.

    Output of the static toDebug method
    You now have an easy way to dump any object you create to the Developer Console!

4 comments:

  1. Hi,

    i have a method which contains only public static strings how do i write a test class to civer this code.

    ReplyDelete
  2. This can be done by just instantiating the class.

    ReplyDelete
  3. Hi,

    How can i use static variable in non static method ? Is it possible ?

    ReplyDelete