{"id":1240,"date":"2026-09-03T19:00:00","date_gmt":"2026-09-03T13:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/"},"modified":"2026-09-03T19:00:00","modified_gmt":"2026-09-03T13:30:00","slug":"apex-access-modifiers","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/","title":{"rendered":"Apex Access Modifiers: public, private, protected, global"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Access Modifiers decide who can see and use your classes, methods and variables. They are the small words like <code>public<\/code> and <code>private<\/code> at the start of a line. Choosing them well keeps your code safe from misuse and makes it clear which parts others should call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You have typed <code>public<\/code> and <code>private<\/code> many times already. They appeared in <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-methods-parameters-return-types\/\">Apex methods<\/a> and in the <code>CourseBatch<\/code> class from <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-classes-and-objects-constructors\/\">Apex classes and objects<\/a>. Now we explain exactly what each modifier does. You will also learn <code>protected<\/code> and <code>global<\/code>, which show up in interviews and in managed packages. If you want a refresher on how orgs and packages fit together, see <a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-appexchange-install-apps\/\">installing AppExchange apps<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Apex Access Modifiers?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An access modifier is a keyword that sets the visibility of a class member. Visibility means which other code may read a variable or call a method. Apex has four access modifiers: <code>private<\/code>, <code>protected<\/code>, <code>public<\/code> and <code>global<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of rooms in a school. Only teachers enter the private staff room. Next, a protected classroom lets in students of that class and their teacher. Then the public school hall is open to anyone in the school. Finally, the global front gate area welcomes even visitors from outside.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Access Modifiers Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Without Apex Access Modifiers, any code could change any value. That quickly leads to bugs that are hard to trace. Apex Access Modifiers give you clear boundaries.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Protect data.<\/strong> Outside code cannot set a private seat count to a negative number.<\/li>\n\n<li><strong>Show intent.<\/strong> Public methods tell teammates &#8220;call me&#8221;, while private ones say &#8220;internal only&#8221;.<\/li>\n\n<li><strong>Change safely.<\/strong> You can rewrite private helpers without breaking other classes.<\/li>\n\n<li><strong>Package safely.<\/strong> Global decides what managed package customers can use.<\/li>\n\n<li><strong>Pass reviews.<\/strong> Security reviews and code reviews check that only needed parts are open.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Access Modifiers Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex checks access when it compiles your code. If a class tries to call a private method from another class, the compiler stops with an error. So access problems show up early, long before users see them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1-1024x640.png\" alt=\"Apex Access Modifiers diagram showing private, protected, public and global access levels\" class=\"wp-image-1238\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Each access modifier opens a wider circle of access.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Each modifier opens a wider circle of access. <code>private<\/code> is the smallest circle, only the class itself. <code>global<\/code> is the largest, which includes code outside your namespace.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Default Is private<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you leave out the modifier on a method or variable, Apex treats it as <code>private<\/code>. So <code>Integer count;<\/code> inside a class is private. Many teams still write <code>private<\/code> on purpose, because it makes the intent clear.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Top-Level Classes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A top-level class, the main class in a file, must be <code>public<\/code> or <code>global<\/code>. Test classes are an exception, and they are usually <code>private<\/code> with the <code>@isTest<\/code> label.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Four Apex Access Modifiers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows where each modifier allows access. &#8220;Yes&#8221; means code in that place can use the member.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Modifier<\/th><th>Same class<\/th><th>Inner and child classes<\/th><th>Other classes in your org<\/th><th>Other namespaces<\/th><\/tr><\/thead><tbody><tr><td>private<\/td><td>Yes<\/td><td>Inner classes only<\/td><td>No<\/td><td>No<\/td><\/tr><tr><td>protected<\/td><td>Yes<\/td><td>Yes<\/td><td>No<\/td><td>No<\/td><\/tr><tr><td>public<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>global<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">private<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>private<\/code> members are visible only inside the class that defines them, including its inner classes. Use it for helper methods and internal data. Most of your variables should be private.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">protected<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>protected<\/code> members are visible to the class, its inner classes and any class that extends it. You can only use it on instance methods and member variables, not on static ones. It matters when you build base classes for others to extend.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">public<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>public<\/code> members are visible to any Apex code in your org or in the same namespace. This is the normal choice for classes and methods that other code needs to call.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">global<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>global<\/code> members are visible to code in any namespace. You need it for managed packages and for some web services. However, once a global method ships in a managed package, you cannot remove or rename it easily. So use <code>global<\/code> only when you truly need it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Access Modifiers Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s use Apex Access Modifiers in a small fee module for Bright Path Tutors. The base class <code>FeeRule<\/code> holds shared logic. A child class <code>OnlineFeeRule<\/code> adds an online discount. Each member uses the narrowest modifier that works.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing virtual class FeeRule {\n\n    private static final Decimal TAX_RATE = 18;   \/\/ internal only\n    protected Decimal baseFee;                    \/\/ children can use it\n\n    public FeeRule(Decimal baseFee) {\n        this.baseFee = baseFee;\n    }\n\n    public Decimal finalFee() {                   \/\/ anyone can call\n        return addTax(priceBeforeTax()).setScale(2);\n    }\n\n    protected virtual Decimal priceBeforeTax() {  \/\/ children may override\n        return baseFee;\n    }\n\n    private Decimal addTax(Decimal amount) {      \/\/ helper, hidden\n        return amount + (amount * TAX_RATE \/ 100);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>TAX_RATE<\/code> and <code>addTax<\/code> are private, so no other class can change the tax maths. Next, <code>baseFee<\/code> and <code>priceBeforeTax<\/code> use protected access, so child classes can use and change them. Only the constructor and <code>finalFee<\/code> are public, because those are the parts outside code needs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the child class in its own file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class OnlineFeeRule extends FeeRule {\n\n    public OnlineFeeRule(Decimal baseFee) {\n        super(baseFee);\n    }\n\n    protected override Decimal priceBeforeTax() {\n        return baseFee * 0.9;   \/\/ 10% online discount\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The child calls the parent constructor with <code>super<\/code>. Then it overrides the protected method to apply a discount. It can read <code>baseFee<\/code> thanks to its protected access. It cannot touch <code>TAX_RATE<\/code>, because that one is private.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now test both from anonymous Apex.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FeeRule classroom = new FeeRule(6000);\nFeeRule online = new OnlineFeeRule(6000);\n\nSystem.debug('Classroom: ' + classroom.finalFee());\nSystem.debug('Online: ' + online.finalFee());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The log shows the classroom fee with tax, and the online fee with a discount and tax.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [4]|DEBUG|Classroom: 7080.00\nUSER_DEBUG [5]|DEBUG|Online: 6372.00<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you try <code>classroom.addTax(100)<\/code> in the same script, the compiler refuses. The message says the method is not visible. That is the access modifier doing its job.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">public vs global<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These two are the most often confused Apex Access Modifiers. The table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>public<\/th><th>global<\/th><\/tr><\/thead><tbody><tr><td>Visible in your org<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Visible to other namespaces<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Needed for managed package APIs<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Easy to change later<\/td><td>Yes<\/td><td>No, once released in a package<\/td><\/tr><tr><td>Typical use<\/td><td>Normal org code<\/td><td>Package APIs and some web services<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is clear for most developers. Use <code>public<\/code> for normal code in your org, and use <code>global<\/code> only when code from another namespace must call it.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2-1024x640.png\" alt=\"Apex Access Modifiers diagram comparing public and global\" class=\"wp-image-1239\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">public vs global in Apex.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Access Modifiers vs Other Class Keywords<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Beginners often confuse access modifiers with other keywords that appear in the same spot. These keywords do different jobs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>virtual<\/code> lets other classes extend or override a class or method. Next, <code>abstract<\/code> stops anyone creating the class directly, and it allows methods with no body. Then <code>override<\/code> marks a method that replaces a parent&#8217;s virtual method. Finally, <code>with sharing<\/code> controls record access for the running user, not code visibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So a line like <code>public with sharing virtual class FeeRule<\/code> combines several ideas. <code>public<\/code> is the only Apex Access Modifier in it. The other words control sharing and inheritance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Private Members with @TestVisible<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Private methods create a small problem for testing. Your test class cannot call them, because the modifier hides them. Apex solves this with the <code>@TestVisible<\/code> annotation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@TestVisible\nprivate Decimal addTax(Decimal amount) {\n    return amount + (amount * TAX_RATE \/ 100);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With this label, test classes can call <code>addTax<\/code> directly. Normal code still cannot see it, so your design stays clean. Use <code>@TestVisible<\/code> instead of making a method public just to test it. That way, Apex Access Modifiers keep protecting your real callers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Access Modifiers and Components<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When a Lightning Web Component calls Apex, the method needs two things. It must be <code>public<\/code> or <code>global<\/code>, and it must have the <code>@AuraEnabled<\/code> annotation. It must also be <code>static<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, <code>@AuraEnabled public static List&lt;Course__c&gt; getCourses()<\/code> is a typical method a component calls. If you mark it <code>private<\/code>, the component cannot reach it. We will cover this in the LWC level, but the rule comes straight from Apex Access Modifiers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Choosing the Right Modifier<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A simple rule works well: start with the smallest access and open it only when needed. Begin with <code>private<\/code>. If a child class needs it, change it to <code>protected<\/code>. If another class needs it, make it <code>public<\/code>. Reach for <code>global<\/code> only for packages or special web services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This habit keeps your public surface small. As a result, fewer things can break when you change your code later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a quick way to decide during code reviews. For each public method, ask who calls it today. If the answer is &#8220;only this class&#8221;, make it private. If the answer is &#8220;only child classes&#8221;, make it protected. This small review often removes half the public methods in a beginner&#8217;s class, and the code gets easier to change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Access Modifiers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Making everything <code>public<\/code> by habit. It exposes internal details that others then depend on.<\/li>\n\n<li>Using <code>global<\/code> in normal org code. It adds no value and makes future changes harder in packages.<\/li>\n\n<li>Trying to use <code>protected<\/code> on a static method. Apex only allows it on instance members.<\/li>\n\n<li>Forgetting the default is <code>private<\/code>, then wondering why another class cannot call a method.<\/li>\n\n<li>Confusing <code>with sharing<\/code> with access. Sharing controls records, not who can call your code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Access Modifiers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start private and widen access only when a real caller needs it.<\/li>\n\n<li>Write the modifier explicitly, even when private is the default, so intent is clear.<\/li>\n\n<li>Keep member variables private, and expose them through methods or read-only properties.<\/li>\n\n<li>Use <code>protected<\/code> with <code>virtual<\/code> methods when you design classes for extension.<\/li>\n\n<li>Review every <code>global<\/code> member carefully before you release a managed package.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a child class <code>PremiumFeeRule<\/code> that extends <code>FeeRule<\/code> and adds 500 to the base fee before tax. Then print its final fee for a base of 6000. Hint: override <code>priceBeforeTax<\/code> and return <code>baseFee + 500<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_classes_access_modifiers.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Access Modifiers<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_classes_extending.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Extending a Class<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_database\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Apex Basics and Database<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Access Modifiers control who can use a class, method or variable.<\/li>\n\n<li>The four modifiers are private, protected, public and global.<\/li>\n\n<li>Members with no modifier are private by default.<\/li>\n\n<li>Protected works only on instance members and opens access to child classes.<\/li>\n\n<li>Use global only when code from another namespace must call your code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You have seen <code>static<\/code> in almost every example so far. Next, learn exactly what it changes in <a href=\"https:\/\/swapnilzambare.com\/blog\/static-vs-instance-apex\/\">static vs instance members in Apex<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Access Modifiers decide who can see and use your classes, methods and variables. They are the small words like public and private at&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1237,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,15,7,97],"swapz_series":[],"class_list":["post-1240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-beginner","tag-salesforce","tag-salesforce-developer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Access Modifiers: public, private, global<\/title>\n<meta name=\"description\" content=\"Apex Access Modifiers explained: private, public, protected and global, the default access, and when to use each one in Salesforce code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Access Modifiers: public, private, global\" \/>\n<meta property=\"og:description\" content=\"Apex Access Modifiers explained: private, public, protected and global, the default access, and when to use each one in Salesforce code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-03T13:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"Apex Access Modifiers: public, private, protected, global\",\"datePublished\":\"2026-09-03T13:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/\"},\"wordCount\":1527,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-access-modifiers-featured.png\",\"keywords\":[\"Apex\",\"Beginner\",\"Salesforce\",\"Salesforce Developer\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/\",\"name\":\"Apex Access Modifiers: public, private, global\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-access-modifiers-featured.png\",\"datePublished\":\"2026-09-03T13:30:00+00:00\",\"description\":\"Apex Access Modifiers explained: private, public, protected and global, the default access, and when to use each one in Salesforce code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-access-modifiers-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-access-modifiers-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Access Modifiers featured image: title card with access modifiers in code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-access-modifiers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Access Modifiers: public, private, protected, global\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Access Modifiers: public, private, global","description":"Apex Access Modifiers explained: private, public, protected and global, the default access, and when to use each one in Salesforce code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/","og_locale":"en_US","og_type":"article","og_title":"Apex Access Modifiers: public, private, global","og_description":"Apex Access Modifiers explained: private, public, protected and global, the default access, and when to use each one in Salesforce code.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-03T13:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"Apex Access Modifiers: public, private, protected, global","datePublished":"2026-09-03T13:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/"},"wordCount":1527,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-featured.png","keywords":["Apex","Beginner","Salesforce","Salesforce Developer"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/","name":"Apex Access Modifiers: public, private, global","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-featured.png","datePublished":"2026-09-03T13:30:00+00:00","description":"Apex Access Modifiers explained: private, public, protected and global, the default access, and when to use each one in Salesforce code.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-access-modifiers-featured.png","width":2400,"height":1260,"caption":"Apex Access Modifiers featured image: title card with access modifiers in code"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-access-modifiers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Access Modifiers: public, private, protected, global"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1240"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1240\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1237"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1240"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}