• #21614

    in which scenario, we should use static method in VF page contoller?

    my controller is not giving right output if i am using  all methods and variables static in my code.

    • This topic was modified 11 months, 1 week ago by  saloni gupta.
    Salesforce | Shaharyar Shaharyar #462

    Hello Saloni,

    #Static Method :

    Static methods are methods that act globally and not in the context of a particular object of a class. Being global, static methods only have access to its provided inputs and other static (global) variables.

    This example illustrates static vs. non-static methods
    public class LightJedi {
    public void slashLightsaber() {
    // Vooooom!

    public static String getJediCode() {
    String code = ‘There is no emotion, there is peace.’;
    code += ‘There is no ignorance, there is knowledge.’;
    code += ‘There is no passion, there is serenity.’;
    code += ‘There is no death, there is the Force.’;
    return code;

    public static String describeRank(String rank) {
    String description;
    if (rank == ‘Initiate’) {
    description = ‘Jedi hopeful’;
    } else if (rank == ‘Padawan’) {
    description = ‘Apprentice of a Jedi Knight’;
    } else if (rank == ‘Knight’) {
    description = ‘Completed the Jedi Trials’;
    } else if (rank == ‘Master’) {
    description = ‘David Liu’;
    return description;

    // Elsewhere…
    LightJedi davidLiu = new LightJedi();

    // Non-static methods require an object for context

    // Static methods aren’t related to a particular object

    // You don’t even use an object to call a static method!
    System.debug(LightJedi.describeRank(‘Master’)); // David Liu

    Other examples of static methods could be convert millimeters to centimeters, subtract two numbers, and convert String to uppercase.

Viewing 2 posts - 1 through 2 (of 2 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?