You need to sign in to do that
Don't have an account?
Problem with getter
Hi,
I want to know about the setter and getter. I know that it is a properties of declaring variable but some time I get confused. Presently I am using a sample of code that is below:
public with sharing class hello
{
public String temp{set;get;}
public hello(){
temp = 'first' ;
}
public String gettemp(){
return 'second' ;
}
}
So I want to know that when I use temp on VFP than what value will show on page..... and what is reason of that ...............
what is the difference of temp and gettemp .....
Thanks
Ashlekh Gera
This is the best resource that I used to understand get and set method
http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html
To answer your question about temp car disply
When you assign value of u r temp variable to lets say an input box you will call get method and the value will come from there
Hope this helps.
Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, you define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner, you have control over the public interface.
When you define a variable like this eg. String temp{set;get;} compiler actually treats it as two virtual methods also created in the code. These methods are
public String gettemp() {
return temp;
}
and
public void settemp(String temp) {
this.temp = temp;
}
so that when you want to get the value of temp or you want to set the value of temp from any other class/code you would be able to do that.
Thanks
Utsav
[Do mark this answer as solution if it works for you and give a kudos.]
simply..
understand TWO directions i.e. From source TO Destination (and) From Destination TO Source
Set is used to ASSIGN value in the controller or FROM VisualForce Page TO APEX Controller.
Get is used to SEND i.e. display the value FROM Apex Controller TO VisualForce Page.
That's about the Set & Get methods used separately. I hope you already know the usage.
And..Coming to Property. Property contains both the methods, where we can reduce NumberOfLinesOfcode in the Controlelr.
Once you are defining
We should have the clarity for the usage of the Variable i.e. is it only for "get" (or) for "get" and "set"
1. If your requirement is only for RETREIVING i.e. Controller to VF Page, and sure not taking the value from VF Page to Apex Controller,
use like below which is GET method only.
2. And, if your requirement is for RETREIVING i.e. Controller to VF Page AND also ASSIGNING i.e. from VF Page to Apex Controller, But no initial value on the vf page to the variable, Try the following; which is GET & SET, but not initial SET value
3. And, if your requirement is for RETREIVING i.e. Controller to VF Page AND also ASSIGNING i.e. from VF Page to Apex Controller, But no initial value on the vf page to the variable, Try the following; which is GET & SET. And. also to initially SET value
VisualForce page code :
I hope you understand w/o confusion, feel free if you require further understanding..
Thanks to all to reply this question .
But my problem is still there.
What is the problem exactly???????????
What is not cleared???????????????
Hi,
I am working on a project in which the code is written like my example so i want to understand why they code like this..
and other problem is that if we are declaring a var like this.
private String temp ;
and last we make a getter method and setter method like this
public String gettemp(){ return '';}
public Void settemp(String value){ a= value;}
Then what is mean to declare string temp as a private