VISUALFORCE : Get () METHOD
Visualforce : Get () Method
In this episode, we are gonna discuss about very interesting as well as important topic i.e. Visualforce with its two fantastic methods.
When you want to refer apex class variables in the visualforce page we need to use getter and setter methods.
get() Method :
When visualforce page wants to get the value of a variable defined in the apex class. It will invoke the get() method of that variable.
For Example :
=============================================
<apex:outputlabel > {!Name} </apex:outputlabel>
=============================================
In above syntax, {!Name} is a variable defoned in apex class.
In the above statement, a visualforce page is trying to use Name variable which is declared in apex class. So it will automatically invoke getName() method in the apex class.
This method will return the value of Name.
Let's understand with the help of simple apex class.
For Example :
=============================================
public class Example
{
string Name ;
public void set(string Name) //Setter method: This will take the value from the visualforce page and stores to apex variable Name.
{
this.Name = Name ;
}
public string getName() //Getter method: This method will return a value to a visualforce page whenever a Name variable is called.
{
return Name ;
}
}
=============================================
In the above class, we used set() and get() methods.
Now we will take an example for the getter method using visualforce and apex class.
Example Class :
=============================================
public class Example
{
string Name ;
public string getName()
{
return 'Salesforcekid' ;
}
}
=============================================
Now let's add this variable to visualforce page.
Example Visualforce Page :
Example Visualforce Page :
=============================================
<apex:page controller="Example"> //here controller is pointing to class Example
<apex:outputlabel> {!Name} </apex:outputlabel> //Requesting to get value stored inside Name variable.
</apex:page>
=============================================
In the above code, when the page is loaded first it creates an object for the Example class.
When outputlabel calls {!Name} in the visualforce page. It invokes getName() method in the controller class.
F
F
finally, it will return "Salesforcekid".
Output :
=============================================
=============================================
Now as I mentioned at the start, In the previous EPISODE we understood how to create a constructor in apex class.
Let's take one more example of visualforce like the above. This time we will define a constructor in the apex class.
Apex Class of Example 2:
=============================================
public class Example2 //When visualforce page is loaded it creates an object for the Example2 class.
{
string Name ;
public Example2() //At the time of creating the object if first calls the constructor and assign Name= "SalesforceKid".
{
Name = "SalesforceKid" ;
}
public string getName()
{
return Name ; //When visualforce page call {!Name} it invokes getName() in the apex class.
}
}
=============================================
Visualforce Page of Example 2:
=============================================
<apex page controller="Example2">
<apex:outputlabel>
My name is {!Name}
</apex:outputlabel>
</apex:page>
=============================================
Output :
=============================================
=============================================
WOHOOO !! YOU HAVE JUST COMPLETED VISUALFORCE: GET () METHOD EPISODE.
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you
Happy learning ☁️⚡️ (Learn. Help. Share.)
VISUALFORCE : Get () METHOD
Reviewed by
on
Rating:
Loved it. Simple and easy to understand. Waiting for more episodes...
ReplyDeleteIn Example 2, when does "public Example2()" happen?
ReplyDeleteIs it when "" happens?
Or when "public class Example2" happens?
Or maybe "string Name ;" happens?
Or something else altogether.
If you your query is when does public Example2() will then the answer is whenever the class is called by default it will load the value from the constructor as constructor load at the time of initialisation.
Delete