• #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.

    #21620
    Salesforce | Shaharyar Shaharyar #8

    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
    davidLiu.slashLightsaber();

    // Static methods aren’t related to a particular object
    LightJedi.getJediCode();

    // 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.

CONTACT US

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

Sending

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

Property of Forcetalks.com
Copyright 2017 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?