3

Till now my idea was static and new keyword are independent but when using testclasses I've found new concepts which are confusing See below class , We've to declare list as static to use it in static method but using static and initializing it with new keyword is confusing

I'm not finding any other mean besides using 'static keyword' , when using list inside methods meant for test classes

  1. I 'm also finding I can 'add' static as well as non static accounts inside this static list. what does that mean? (see statements under 'try' block below)

    public class staticConcetps
    
    {
       public static integer iStatic = 1;
       public static Account accStatic;
       public static list<Account> AccListstatic = new list<Account>();
         // Not able to understand this, static as well as new keyworld used
    
       public integer iNonStatic;
       public Account accNonStatic;
    
       public static void Funcstatic()
       {
        try
         {
            // adding non static account
              account accobj = new Account();
             accobj.name = 'shekhar Acc1';
              AccListstatic.add(accObj);             
    
            // adding static account
             AccStatic = new Account();
             accStatic.name = 'Account Static insert1';
             AccListstatic.add(accStatic);
           insert AccListstatic ;
    
         }
         catch(system.Exception d)
         {
           system.debug('ExceptionMessage=' + d.getMessage());
           system.debug('ExceptionLineNumber=' + d.getLineNumber());
         } 
    

    } }

1

In what way or why do you say that the static and the new are independent? You could declare something like this:

public class staticConcepts
{
    //at this point the list is null. You cannot call methods associated to the list.
    public static List<String> staticList; 

    //This line of code says I will initialize a new instance of the List class 
    //and store inside my container. That container is a static list.
    staticList = new List<String>(); 

    //You can then use the various methods
    staticList.add('a');    
}

Here is the List Class

  • my questions is to call static variables ( say static integer i), I don't need to instantiate class. I can refer directly using syntax -className. VariableName But for list, I've to instantiate first . even I declare it static. This mean 'static collections' are different than normal static variables. They need to be instantiated first. which is why it is confusing to me. Also in static list, I can add both static and non static account. . would that cause any trouble? As Inserting such list cause no issues and works fine. – curious_techy Feb 5 '15 at 4:21
  • 1
    You can access the list just like a static variable, without instantiating the class. If you have a static integer, you still need to assign it a value, it's the same with list ...you need to assign a value to it in order to work with it. Think of them as containers, you still need to assign values from integers or strings to list or interfaces. – Mihai Neagoe Feb 5 '15 at 7:55
1

Salesforce documentation is clear about that:

When you declare a method or variable as static, it's initialized only once when a class is loaded.

It means that static variables are initialized when the class is accessed for the first time. You should think of a static variable as of the instance that belongs to the class itself, so there is no reason it couldn't be instantiated.

Answering your second question:

What you are actually doing is adding a local variable to static list which affect the variable's lifetime. It's perfectly legal as there is no constraint to add only static objects/fields to static collection.

1

I guess the confusion is between how primitive data types are different from collections. Collections are not the actual value holders (pointers from the C/C++ days), they are mere pointers to pointers (for in-depth details refer http://www.ibm.com/developerworks/library/j-codetoheap/)

Irrespective of being a static variable or not, you always have to initialize a collection, but this is not required in case of a primitive data type (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm).

Making a variable/ collection static just affects its accessibility and life, its value extraction mechanism is still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.