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
AshlekhAshlekh 

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

 

nikvmnikvm

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.

 

imutsavimutsav
D-Horse in your example you have created a method gettemp() which is not needed since you have already defined public String temp{set;get;}

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.]
ShahTheTrainerShahTheTrainer

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

 

Public String Temp { set; get; }  //means you are also defining as below :
	
	Public String Temp { set
				{
				}
			     get
				{
				}
			   }

 




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.

Public String getTemp()
     {
         return StirngVariable/'some text here';
     }

 

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

Public String Temp 
 { 
   set
     {
     }
   get
     {
       return StirngVariable/'some text here';
     }
  }

 

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

Public String TempSet = 'Initial Value';   
// declaring variable and assigning value
// also remember you can use this variable on VF page, because there is no 'get'

   Public String Temp { set
			{
			   value = TempSet;  // assigning the value from another variable. this is initial value.
			}
			get
			 {
			   return TempSet;   // the value is returned to Temp variable on the VF page.
			 }
		       }

 


VisualForce page code :

<apex:page>
    <apex:form>
	Initial value of Temp Variable = {!Temp }    <!-- you will see "Initial Value" that we assigned to TempSet in the controller -->
    <br />
	Enter your Text to assingn it to Temp Variable :     <apex:inputText value="{!Temp}" />
      <apex:commandButton value="click to see the entered Text" />
				Entered Text Value of Temp Variable after CLICKING the button : {!Temp}
	</apex:form>
</apex:page>

 



I hope you understand w/o confusion, feel free if you require further understanding..

AshlekhAshlekh

Thanks to all to reply this question .

But my problem is still there.

 

ShahTheTrainerShahTheTrainer

What is the problem exactly???????????

 

What is not cleared???????????????

AshlekhAshlekh

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