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
prasanth puvvada 4prasanth puvvada 4 

how to initialize a method without constructor.

sir, i am using list program.  This is a apex class program.  here i am getting all the colors vales at VF page. VF page and Apex page is working well.  here my doubt is how can i use this apex program without constructor.  please help. thanks in advance. 
 
public class listcolor
{
public list<string> color{set;get;}

public listcolor()
{
color=new list<string>{'red','white','green'};

list<string> myval=new list<string>();
myval.addall(color);

}
}

I tried this, but it is giving error. 
 
public class listcolor
{
public list<string> color{set;get;}

mycolors();


public void mycolors()
{
color=new list<string>{'red','white','green'};

list<string> myval=new list<string>();
myval.addall(color);

}
}

OR OR OR OR OR OR OR OR OR OR OR OR OR OR OR OR OR OR 





public class listcolor
{
public list<string> color{set;get;}

public list<string>  mycolors()
{
color=new list<string>{'red','white','green'};

list<string> myval=new list<string>();
myval.addall(color);

return color;

}
}

Vf page ( for your reference):- 
 
<apex:page controller="listcolor" title="list of colors">
<apex:form>
<apex:pageblock>

<apex:pageblocktable var="a" value="{!color}">
<apex:column headervalue="colors" value="{!a}" />

</apex:pageblocktable>
</apex:pageblock>


</apex:form>
</apex:page>

 
Best Answer chosen by prasanth puvvada 4
justin_sfdcjustin_sfdc
Hi prasanth,

Typically, using a constructor is a way to go. However if you do not want to use the constructor then you would have to call the controller method from your visualforce page so that the method gets executed.

Hope that helps!

All Answers

justin_sfdcjustin_sfdc
Hi prasanth,

Typically, using a constructor is a way to go. However if you do not want to use the constructor then you would have to call the controller method from your visualforce page so that the method gets executed.

Hope that helps!
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
HI prasanth puvvada 4,

According to VF page life cycle :-

Constructor of controller /extension - >  constructor of custom components  -> AssignTo any Custom Components -> Action method->Getter or Setter -> Form method -> HTML

http://amitsalesforce.blogspot.in/2015/04/visualforce-page-life-cycle-in.html

if you dnt want to call constructor then you can try action method.
<apex:page controller="listcolor" title="list of colors" action="{!mycolors}">
But as best practice always use constructor and use action method only when you need to do any DML while loading the VF page.

You Can try beloe code without constructor
<apex:page controller="listcolor" title="list of colors" action="{!mycolors}">
<apex:form >
<apex:pageblock >

<apex:pageblocktable var="a" value="{!color}">
<apex:column headervalue="colors" value="{!a}" />

</apex:pageblocktable>
</apex:pageblock>


</apex:form>
</apex:page>
public class listcolor
{
    public list<string> color{set;get;}

    public void mycolors()
    {
        color=new list<string>{'red','white','green'};
        
        list<string> myval=new list<string>();
        myval.addall(color);
        
    }
    
}
Please mark this as solution if this will help you

Thanks
Amit Chaudhary
Amit Chaudhary 8Amit Chaudhary 8
One more Option for you if you dnt want to call constructor then you can user getter method also.

Try below code:-
public class listcolor
{
public list<string> color;

    public list<string> getColor()
    {
        color=new list<string>{'red','white','green'};
        return color;
    }
    
}
<apex:page controller="listcolor" title="list of colors">
<apex:form>
<apex:pageblock>

<apex:pageblocktable var="a" value="{!color}">
<apex:column headervalue="colors" value="{!a}" />

</apex:pageblocktable>
</apex:pageblock>


</apex:form>
</apex:page>


Below is Order of execution for VF page :-

1) Constructor of controller /extension
2) constructor of custom components 
3) AssignTo any Custom Components
4) Action method
5) Getter or Setter
6) Form method -> HTML


If you dnt want to use constructor  then you can use action Method of getter/setter direclty

Hope this will help you.