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
sreenivas_cippyalisreenivas_cippyali 

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

without creating object can we use it?

MandyKoolMandyKool

Hi,

 

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

 

eg.

 

public class MyClass

{

public static void myMethod()

        {

           // Your code here

        }

}

 

// Call it from any other class as follows:

 

MyClass.myMethod();

 

Hope this is what you are looking for!!

ShamilShamil

It depends whether your methods are static or not.

E.g. 

 

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:

TestGlobalClass.staticMethod();

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

TestGlobalClass tgc = new TestGlobalClass();
tgc.nonStaticMethod();

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

sreenivas_cippyalisreenivas_cippyali

 

global methods are used in any other class right.

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

SFDC_EvolveSFDC_Evolve

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

 

 

Thanks 

SFDC_Evolve

 

ShamilShamil

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


Link:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_access_modifiers.htm?SearchType=Stem

 

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.