function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SameerC77SameerC77 

difference between transient instance variable and static variable

View state is not maintained for transient instance variables  and static variables also. Then what exactly is the difference between transient instance variable and  static variable ?

gautam_singhgautam_singh

Hi ,


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. All static member variables in a class are initialized before any object of the class is created.  Indeed they aren't transmitted as part of the view state for a Visualforce page.
Using static variables will cause only one instance of the variable to be loaded when the application loads and that variable will not go out of memory until the app is closed.  It holds information that is common to all instances on a class and It is shared between them instead of being created a new with each instance.


while


Transient keyword to declare instance variable that can not be saved and should not transmitted as part of view state for visual force page.
Basically ,View State is an encrypted, hidden <input> field on a Visualforce page that keeps track of Apex controller state & Visualforce page state between server requests. This field is only generated when there is an <apex:form> tag present on a page.


ViewState is only used on a single page that handles postbacks. Once you redirect to the new page, the ViewState is lost.




The Whole Process of Calling is like this :


A. URL Requested
B. Apex Controller Instantiated on Server
C. Controller State Serialized & Encrypted to View State
D. Page Markup Sent to Browser & Rendered
E. View State Decrypted & Deserialized (for Postbacks)



View State Automatically keeps track of field values for you and Allows for easy AJAX functionality. In order to remove View State Error we use 4 methods.


A. Reduce Number of Components
B. Use the transient Keyword
C. Use JavaScript Remoting
D. Use the Streaming API


Declare only those fields as transient which values you dont need in visualforce page.

Click here for more Details about Transient & Static.



Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You .

sukruth555sukruth555
When is the need to use static and when is the need to use transient in vf page?
 
Akshay Jain 109Akshay Jain 109
Nice post. Very helpful. Thanks.