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
David WrightDavid Wright 

What is the real impact of using the "global" keyword in a class definition?

I'm having trouble understanding what the global keyword actually does does in Apex. I know what the docs say, so I'm not looking for anyone to spit that out. The docs say:

"The global access modifier declares that this class is known by all Apex code everywhere. All classes that contain methods defined with the webService keyword must be declared as global. If a method or inner class is declared as global, the outer, top-level class must also be defined as global."

The phrase "...known by all Apex code everywhere." is confusing to me, and possibly even a little scary. Is it suggesting that using the global keyword allows your code to be visible across other orgs (literally ALL apex code, at least for that SFDC instance), or is it just visible to all apex in your org, and if so, how is that different from simply using the public keyword?
Here-n-nowHere-n-now
It simply indicates where the class is referenceable/invokable.  Global is more than Public because it crosses namespace boundaries, i.e., the code in other namespaces get to use the definition as well.  This is probably most applicable in terms of apps, which are required to be in separate namespaces.

The access modifier really has nothing to do with the source code visibility by the way.  Declaring a class to be Global won't make the the source code visible by a 3rd party (for instance, your favorite app publisher).  It simply means if the code in those app namespaces wants to invoke the available Global methods in your code, it could.
David WrightDavid Wright
No concern about source code visibility--I know it doesn't do that.

My question is more about whether the functions are somehow invokable across all of apex globally, or if it's restricted within your org. From your answer, it seems like the only real purpose of its usage (besides the required usage for web services) would be for app developers to allow their code to be invoked outside of their apps namespace. I figured that was the case.

Still leaves an unanswered question (based on hazy wording in the documentation) about exactly how far global goes. Is it only applied to the apex (Accross all namespaces) within one org, or does it cross org boundaries?
Here-n-nowHere-n-now
It does not cross org boundaries.  I actually don't know a formal documentation reference for that, but it does seem to be intuitively so to me.  To make things referenceable across org boundary, Apex has to either enforce name uniqueness (for the default name space at least) across all orgs, or support namespace hierarchy.  Neither is present in Apex.