+ Start a Discussion
vutukuru balajivutukuru balaji 

static keyword usage

Public Class Example
{
public static Integer a;
public integer b;
public Example()
{
a=10;
b=10;
}
public void add()
{
a=a+10;
b=b+10;
System.debug('a value is '+a);
System.debug('b value is '+b);
}
}


in the anonymous class

Example e1= new Example();
e1.add();
Example e2= new Example();
e2.add();


what is use of static keyword???????????
 
Qiu YueNvQiu YueNv
Please refer to the documentation:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
PratikPratik (Salesforce Developers) 
Hi Vutukuru,

The static variable will carry it's value through the program, as Qiu mentikoned the link of article:

Static methods, variables, or initialization code are associated with a class, and are only allowed in outer classes. When you declare a method or variable as static, it's initialized only once when a class is loaded. Static variables aren't transmitted as part of the view state for a Visualforce page.

Instance methods, member variables, and initialization code are associated with a particular object and have no definition modifier. When you declare instance methods, member variables, or initialization code, an instance of that item is created with every object instantiated from the class.

Local variables are associated with the block of code in which they are declared. All local variables should be initialized before they are used.

Thanks,
Pratik