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

how to use global method in other class. Do we have to create object for that class

without creating object can we use it?




You can create a public static method. Then you can access that method from any other class.




public class MyClass


public static void myMethod()


           // Your code here




// Call it from any other class as follows:




Hope this is what you are looking for!!


It depends whether your methods are static or not.



global class TestGlobalClass {
	global static void staticMethod(){
		System.debug('static method');
	global void nonStaticMethod(){
		System.debug('instance method');

To call the static method you wouldn't need an object:


 To call the instance method you'd definitely need an object:

TestGlobalClass tgc = new TestGlobalClass();

 If you are planning to create global methods - make sure you fully understand why you need it. Most of the time public will do



global methods are used in any other class right.

static methods also we can use , then what is the use of global ?


I think Global Method can be called outside the Org.. they are mainly used for webservice ...  For ORg You can make a method Public .... Static does not make sense for Accessiblity .. 







Quotes from the documentation:


"This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the Web services API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global."


"We recommend using the global access modifier rarely, if at all. Cross-application dependencies are difficult to maintain. "



In other words, unless you'd like to create a managed package and would like others to be able to call methods from your package don't use 'global' modifier.


Hope this helps.