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
Chitral ChaddaChitral Chadda 

What is the difference between static , public and private members of a class,?

What is the difference  between static , public and private members of a class,? when we invoke class from trigger? Can anyone pls explain this with the help of an example.
when we invoke class with (static , public, private member) from trigger
Best Answer chosen by Chitral Chadda
pconpcon
These are all the same as when refering to Java classes [1][2].

static
Static methods (and variables) can be used without instantiating a new instance of the class
 
public class Utils {
    public static String getHelloWorld() {
        return 'Hello World';
    }
}

These methods can be called directly
 
String salutation = Utils.getHelloWorld();
 
non-static
Non-static method (and variables) must have a new instance of the class instantiated in order to be used.  Typically these rely on data inside the class that then is refered to inside the class
 
public class Utils {
    private lang;

    public Utils() {
        lang = 'en';
    }

    public Utils(String lang) {
        this.lang = lang;
    }

    public String getHelloWorld() {
        if (lang == 'en') {
            return 'Hello World';
        } else if (lang == 'es') {
            return 'Hola Mundo';
        }

        // Return esperanto if we don't know the language
        return 'Saluton Mondo';
    }
}

This version of the method requires us to create a new instance
 
Utils en_utils = new Utils();
String salutation_en = en_utils.getHelloWorld();
// salutation_en == 'Hello World'

Utils es_utils = new Utils('es');
String salutation_es = es_utils.getHelloWorld();
// salutation_es == 'Hola Mundo'

Utils fr_utils = new Utils('fr');
String salutation_fr = fr_utils.getHelloWorld();
// salutation_fr == 'Saluton Mondo'
In this example, we can see that we have a private class level variable lang that we use inside of getHelloWorld.  Because this variable is private, we cannot reference it outside of class level variables.  We can mix and match these.  For example, we can have a public class level method call a private class level method that then uses / sets both private and public variables.  We can only call public variables and methos from outside the class
 

All Answers

pconpcon
These are all the same as when refering to Java classes [1][2].

static
Static methods (and variables) can be used without instantiating a new instance of the class
 
public class Utils {
    public static String getHelloWorld() {
        return 'Hello World';
    }
}

These methods can be called directly
 
String salutation = Utils.getHelloWorld();
 
non-static
Non-static method (and variables) must have a new instance of the class instantiated in order to be used.  Typically these rely on data inside the class that then is refered to inside the class
 
public class Utils {
    private lang;

    public Utils() {
        lang = 'en';
    }

    public Utils(String lang) {
        this.lang = lang;
    }

    public String getHelloWorld() {
        if (lang == 'en') {
            return 'Hello World';
        } else if (lang == 'es') {
            return 'Hola Mundo';
        }

        // Return esperanto if we don't know the language
        return 'Saluton Mondo';
    }
}

This version of the method requires us to create a new instance
 
Utils en_utils = new Utils();
String salutation_en = en_utils.getHelloWorld();
// salutation_en == 'Hello World'

Utils es_utils = new Utils('es');
String salutation_es = es_utils.getHelloWorld();
// salutation_es == 'Hola Mundo'

Utils fr_utils = new Utils('fr');
String salutation_fr = fr_utils.getHelloWorld();
// salutation_fr == 'Saluton Mondo'
In this example, we can see that we have a private class level variable lang that we use inside of getHelloWorld.  Because this variable is private, we cannot reference it outside of class level variables.  We can mix and match these.  For example, we can have a public class level method call a private class level method that then uses / sets both private and public variables.  We can only call public variables and methos from outside the class
 
This was selected as the best answer
Chitral ChaddaChitral Chadda
Thnks pcon ! But i m confused that why have u declared 2 constructors here public Utils() { lang = 'en'; } public Utils(String lang) { this.lang = lang; }
Chitral ChaddaChitral Chadda
Okay so ur using 1 default constructor and 1 parameterized constructor . Right ?
pconpcon
Correct.  One is parameterized and one is not.  Showing that you can set a private variable to a variable being passed into a public method.  Would have been better to write the constructors like
 
public class Utils {
    private lang;

    public Utils() {
        this('en');
    }

    public Utils(String lang) {
        this.lang = lang;
    }

    public String getHelloWorld() {
        // Code goes here
    }
}

to reuse the parameterized constructor.

If you are happy with my previous answer, please do me a favor and mark it as the "Best Answer" so that it can be removed from the list of un-answered questions.  Additionally, if you could also delete your other post that is the same question as this so that it isn't lingering around.
Chitral ChaddaChitral Chadda
Thnku dear Yes sure pcon i vl mark it I vl b glad if u can xppain the last point , wat about protected member? Can u please help me with protected member , implemntation in code
pconpcon
Protected members can be accessed by classes that extend the class.  Private members cannot be accessed when extending a class.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_access_modifiers.htm