{"id":27056,"date":"2023-11-08T09:13:01","date_gmt":"2023-11-08T03:43:01","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/"},"modified":"2025-01-31T01:04:33","modified_gmt":"2025-01-30T19:34:33","slug":"the-access-modifiers-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/","title":{"rendered":"Access Modifiers in Java"},"content":{"rendered":"\n<p>Java is a popular object-oriented programming language that values security, scalability, and resilience. One of its most essential features is using access modifiers to regulate the visibility and accessibility of variables, classes, and methods.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-and-encapsulation-in-java\/\">Java's encapsulation<\/a> principle relies heavily on access modifiers, which allow programmers to establish boundaries and guarantee data integrity.<\/p>\n\n\n\n<p>Consider collaborating on a project as a team without limitations on who can view or edit each other's code. There would be havoc! As security measures, access modifiers ensure that only the approved code can interact with particular areas of your application.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-title-link\">Java Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>16.05 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>3 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-button\">\n                Learn Java Programming\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.mygreatlearning.com\/blog\/oops-concepts-in-java\/\">OOP Concepts in Java<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-access-modifiers-in-java\">What Are Access Modifiers in Java?<\/h2>\n\n\n\n<p>Java keywords known as \"access modifiers\" control the scope and visibility of variables, <a href=\"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/\">constructors<\/a>, classes, and methods. They are essential for specifying how various sections of your code communicate with one another and with outside elements.<\/p>\n\n\n\n<p>Java offers four different kinds of access modifiers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Public: <\/strong>Accessible from anywhere.<\/li>\n\n\n\n<li><strong>Private:<\/strong> Only members of the defined class can access it.<\/li>\n\n\n\n<li><strong>Protected:<\/strong> Accessible by subclasses in other packages and within the same package.<\/li>\n\n\n\n<li><strong>Default (Package-private): <\/strong>&nbsp;Only accessible within the same package; no specific keyword is needed.<\/li>\n<\/ul>\n\n\n\n<p>By using access specifiers in Java, sensitive data can be protected by carefully comprehending and using these modifications. You can also avoid unintended modifications and improve the code's readability and maintainability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-modifier-types\">Access Modifier Types<\/h2>\n\n\n\n<p>Let's examine each access modifier in more detail, including its traits and real-world applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-public-access-modifier\">1. public Access Modifier<\/h3>\n\n\n\n<p>The modifier <strong>public<\/strong> permits unrestricted access. Regardless of its package, any other class can access any class, method, or variable declared <strong>public<\/strong>.<\/p>\n\n\n\n<p><strong>When to use:<\/strong> When you wish to make globally accessible APIs, constants, or utility methods available.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage com.example;\npublic class Calculator {\npublic int add(int a, int b) {\nreturn a + b;\n}\n}\npackage com.main;\nimport com.example.Calculator;\npublic class Main {\npublic static void main(String&#x5B;] args) {\nCalculator calculator = new Calculator();\nSystem.out.println(&quot;Sum: &quot; + calculator.add(5, 3)); \/\/ Output: Sum: 8\n}\n}\n<\/pre><\/div>\n\n\n<p><strong>Code Explanation:&nbsp;<\/strong><\/p>\n\n\n\n<p>This Java program demonstrates package usage. The Calculator class (in com.example) provides a method to add two numbers. The Main class (in com.main) imports the Calculator, calls its add method, and prints the result (Sum: 8).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-private-access-modifier\">2. <strong>private<\/strong> Access Modifier<\/h3>\n\n\n\n<p>The <strong>private<\/strong> modifier restricts access to the declared class. It is frequently used to build encapsulation by concealing sensitive data and is the most restrictive access level.<\/p>\n\n\n\n<p><strong>When to Use:<\/strong> To guarantee data integrity and safeguard internal class details.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage com.example;\npublic class BankAccount {\nprivate double balance;\npublic BankAccount(double initialBalance) {\nthis.balance = initialBalance;\n}\npublic double getBalance() {\nreturn balance;\n}\npublic void deposit(double amount) {\nif (amount &gt; 0) {\nbalance += amount;\n}\n}\n}\npublic class Main {\npublic static void main(String&#x5B;] args) {\nBankAccount account = new BankAccount(1000);\naccount.deposit(500);\nSystem.out.println(&quot;Balance: &quot; + account.getBalance()); \/\/ Output: Balance: 1500\n}\n}\n<\/pre><\/div>\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<p>This Java program demonstrates object-oriented programming with a BankAccount class. The class has methods to initialize a balance, deposit money, and retrieve the balance. In the Main class, an account is created with an initial balance of 1000, 500 is deposited, and the updated balance (1500) is printed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-protected-access-modifier\">3. <strong>protected<\/strong> Access Modifier&nbsp;<\/h3>\n\n\n\n<p>The <strong>protected<\/strong> modifier permits access to subclasses in other packages and within the same package. The <strong>public<\/strong> and <strong>private<\/strong> access levels are balanced.<\/p>\n\n\n\n<p><strong>When to Use<\/strong>: In inheritance-related situations where subclasses require access to members of parent classes.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage com.example;\npublic class Animal {\nprotected String name;\nprotected void setName(String name) {\nthis.name = name;\n}\nprotected String getName() {\nreturn name;\n}\n}\npackage com.zoo;\nimport com.example.Animal;\npublic class Dog extends Animal {\npublic void display() {\nsetName(&quot;Buddy&quot;);\nSystem.out.println(&quot;Dog\u2019s Name: &quot; + getName());\n}\npublic static void main(String&#x5B;] args) {\nDog dog = new Dog();\ndog.display(); \/\/ Output: Dog\u2019s Name: Buddy\n}\n}\n<\/pre><\/div>\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<p>This Java program demonstrates inheritance and access modifiers. The Animal class (in com.example) has protected methods to set and get the animal's name. The Dog class (in com.zoo) extends Animal, sets the name to \"Buddy,\" and displays it. The output is Dog\u2019s Name: Buddy.<\/p>\n\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.mygreatlearning.com\/blog\/inheritance-in-java\/\">Inheritance in Java<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-package-private-default-access-modifier\">4. Package-Private\/<strong>Default<\/strong> Access Modifier<\/h3>\n\n\n\n<p>The member can only be accessed within the same package if no access modifier is supplied. In Java, this is the default access level.<\/p>\n\n\n\n<p><strong>When to Use:<\/strong> Methods and classes meant to be used exclusively inside a particular package.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage com.example;\nclass Helper {\nvoid displayMessage() {\nSystem.out.println(&quot;This is a package-private method.&quot;);\n}\n}\npublic class Main {\npublic static void main(String&#x5B;] args) {\nHelper helper = new Helper();\nhelper.displayMessage(); \/\/ Output: This is a package-private method.\n}\n}\n<\/pre><\/div>\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<p>This Java program demonstrates package-private access. The `Helper` class has a method `displayMessage()` with no access modifier, making it accessible only within the same package (`com.example`). The `Main` class calls this method, producing the output: `This is a package-private method.`<\/p>\n\n\n\n<p>For a deeper understanding of how methods are defined and their role in Java, check out our detailed guide on <a href=\"https:\/\/www.mygreatlearning.com\/blog\/methods-in-java\/\">Methods in Java<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comparison-table\">Comparison Table<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Modifier<\/strong><\/td><td><strong>Class<\/strong><\/td><td><strong>Same Package<\/strong><\/td><td><strong>Subclass&nbsp;<\/strong><strong>(Different Package)<\/strong><\/td><td><strong>World<\/strong><\/td><\/tr><tr><td><strong>public<\/strong><\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td><strong>protected<\/strong><\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td><strong>Default<\/strong><\/td><td>Yes<\/td><td>Yes<\/td><td>No<\/td><td>No<\/td><\/tr><tr><td><strong>private<\/strong><\/td><td>Yes<\/td><td>No<\/td><td>No<\/td><td>No<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"best-practices-for-using-access-modifiers\">Best Practices for Using Access Modifiers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Follow the Principle of Least Privilege:<\/strong> Use the most restrictive modifier that allows the desired functionality.<\/li>\n\n\n\n<li><strong>Prevent Overexposure:<\/strong> Use <strong>public<\/strong> only for classes and methods that require worldwide visibility.<\/li>\n\n\n\n<li><strong>Encapsulate Data: <\/strong>Employ <strong>private<\/strong> for fields and use getter and setter methods to grant controlled access.<\/li>\n\n\n\n<li><strong>Leverage Inheritance Wisely: <\/strong>Use <strong>protected<\/strong> for methods and variables intended to be overridden by subclasses.<\/li>\n\n\n\n<li><strong>Document Access Levels: <\/strong>Clearly explain the rationale behind selecting a specific access level, particularly for <strong>public<\/strong> and <strong>protected<\/strong> members.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"common-mistakes-and-how-to-avoid-them\">Common Mistakes and How to Avoid Them<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Overuse of public: <\/strong>Designating all fields and methods as `public` for convenience.\n<ul class=\"wp-block-list\">\n<li><strong>Solution:<\/strong> Determine if the member needs to be reachable outside their class or package.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Forgetting to Define an Access Modifier:<\/strong> Ignoring the need to define an access modifier results in unwanted default (package-private) access.\n<ul class=\"wp-block-list\">\n<li><strong>Solution:<\/strong> For clarity, always provide an access modifier directly.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Misuse of protected: <\/strong>Revealing confidential information via `protected` members.\n<ul class=\"wp-block-list\">\n<li><strong>Solution: <\/strong>Only use `protected` when creating inheritance-friendly designs.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Ignoring Encapsulation: <\/strong>Using `public` fields with getters and setters rather than private fields.\n<ul class=\"wp-block-list\">\n<li><strong>Solution: <\/strong>Always encapsulate fields to maintain data integrity and regulate access.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access-modifiers-with-classes-and-interfaces\">Access Modifiers with Classes and Interfaces<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"class-access-modifiers\">Class Access Modifiers<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Public Class:<\/strong> Openable from any other class.<\/li>\n\n\n\n<li><strong>Package-Private Class:<\/strong> Accessible only within the same package (default).<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ Default (package-private) class\nclass InternalClass {\nvoid display() {\nSystem.out.println(&quot;Accessible only within the package.&quot;);\n}\n}\npublic class PublicClass {\npublic static void main(String&#x5B;] args) {\nInternalClass internal = new InternalClass();\ninternal.display(); \/\/ Output: Accessible only within the package.\n}\n}\n<\/pre><\/div>\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<p>This Java program demonstrates the default (package-private) access modifier. The InternalClass class and its display() method are accessible only within the same package. The PublicClass calls this method, resulting in the output: Accessible only within the package.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"access-modifiers-for-interfaces\">Access Modifiers for <a href=\"https:\/\/www.mygreatlearning.com\/blog\/interface-in-java\/\">Interfaces<\/a><\/h3>\n\n\n\n<p>All methods in an interface are `public` by default, and variables are `public static final`.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic interface Vehicle {\nint MAX_SPEED = 120; \/\/ public static final by default\nvoid start(); \/\/ public by default\n}\npublic class Car implements Vehicle {\n@Override\npublic void start() {\nSystem.out.println(&quot;Car is starting. Max speed: &quot; + MAX_SPEED);\n}\n}\npublic class Main {\npublic static void main(String&#x5B;] args) {\nVehicle car = new Car();\ncar.start(); \/\/ Output: Car is starting. Max speed: 120\n}\n}\n<\/pre><\/div>\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<p>This Java program demonstrates the use of interfaces. The Vehicle interface defines a constant MAX_SPEED and an abstract method start(). The Car class implements the Vehicle interface and provides an implementation for start(). The Main class creates a Car object and calls start(), outputting: Car is starting. Max speed: 120.<\/p>\n\n\n\n<p><strong>Also Read: <\/strong><a href=\"https:\/\/www.mygreatlearning.com\/blog\/abstract-class-vs-interface-in-java\/\">Difference Between Abstract Class and Interface in Java<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"real-world-applications-for-access-modifiers\">Real-world applications for Access Modifiers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>API Development: <\/strong>Private methods manage internal logic, whereas public methods let outside apps communicate with your library.<\/li>\n\n\n\n<li><strong>Frameworks and Libraries: <\/strong>Access modifiers aid in establishing distinct lines between internal implementations and public APIs.<\/li>\n\n\n\n<li><strong>Business Applications:<\/strong> Use access modifiers to guarantee encapsulation and modularity in large-scale systems.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Writing Java code that is efficient, secure, and maintainable requires the use of access specifiers. Sensitive data can be protected by using best practices and comprehending their subtleties. For better results, you should avoid inadvertent actions and improve teamwork among your members.<\/p>\n\n\n\n<p>If you are interested in learning Java from scratch, we have a <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/java-programming\">Free Java Programming Course<\/a> for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a popular object-oriented programming language that values security, scalability, and resilience. One of its most essential features is using access modifiers to regulate the visibility and accessibility of variables, classes, and methods. Java's encapsulation principle relies heavily on access modifiers, which allow programmers to establish boundaries and guarantee data integrity. Consider collaborating on [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":27175,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[],"class_list":["post-27056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.6 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What are access modifiers in Java?<\/title>\n<meta name=\"description\" content=\"Access Modifiers in Java: Access modifiers in java are used to set the accessibility of classes, constructors, and methods of Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Access Modifiers in Java\" \/>\n<meta property=\"og:description\" content=\"Access Modifiers in Java: Access modifiers in java are used to set the accessibility of classes, constructors, and methods of Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-08T03:43:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-30T19:34:33+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Access Modifiers in Java\",\"datePublished\":\"2023-11-08T03:43:01+00:00\",\"dateModified\":\"2025-01-30T19:34:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\"},\"wordCount\":1108,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\",\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\",\"name\":\"What are access modifiers in Java?\",\"isPartOf\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg\",\"datePublished\":\"2023-11-08T03:43:01+00:00\",\"dateModified\":\"2025-01-30T19:34:33+00:00\",\"description\":\"Access Modifiers in Java: Access modifiers in java are used to set the accessibility of classes, constructors, and methods of Java.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage\",\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg\",\"contentUrl\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg\",\"width\":1000,\"height\":667,\"caption\":\"Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/www.mygreatlearning.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\/Software Development\",\"item\":\"https:\/\/www.mygreatlearning.com\/blog\/software\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Access Modifiers in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#website\",\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg\",\"contentUrl\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\",\"https:\/\/x.com\/Great_Learning\",\"https:\/\/www.instagram.com\/greatlearningofficial\/\",\"https:\/\/www.linkedin.com\/school\/great-learning\/\",\"https:\/\/in.pinterest.com\/greatlearning12\/\",\"https:\/\/www.youtube.com\/user\/beaconelearning\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp\",\"contentUrl\":\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\/\/www.mygreatlearning.com\/\",\"https:\/\/in.linkedin.com\/school\/great-learning\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning\",\"https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What are access modifiers in Java?","description":"Access Modifiers in Java: Access modifiers in java are used to set the accessibility of classes, constructors, and methods of Java.","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:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Access Modifiers in Java","og_description":"Access Modifiers in Java: Access modifiers in java are used to set the accessibility of classes, constructors, and methods of Java.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-11-08T03:43:01+00:00","article_modified_time":"2025-01-30T19:34:33+00:00","og_image":[{"width":1000,"height":667,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Access Modifiers in Java","datePublished":"2023-11-08T03:43:01+00:00","dateModified":"2025-01-30T19:34:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/"},"wordCount":1108,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/","name":"What are access modifiers in Java?","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg","datePublished":"2023-11-08T03:43:01+00:00","dateModified":"2025-01-30T19:34:33+00:00","description":"Access Modifiers in Java: Access modifiers in java are used to set the accessibility of classes, constructors, and methods of Java.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg","width":1000,"height":667,"caption":"Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/the-access-modifiers-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Access Modifiers in Java"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg",1000,667,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1-768x512.jpg",768,512,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg",1000,667,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg",1000,667,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1.jpg",1000,667,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1-640x667.jpg",640,667,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_680664070-1-150x100.jpg",150,100,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Java is a popular object-oriented programming language that values security, scalability, and resilience. One of its most essential features is using access modifiers to regulate the visibility and accessibility of variables, classes, and methods. Java's encapsulation principle relies heavily on access modifiers, which allow programmers to establish boundaries and guarantee data integrity. Consider collaborating on&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/27056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=27056"}],"version-history":[{"count":47,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/27056\/revisions"}],"predecessor-version":[{"id":112421,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/27056\/revisions\/112421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/27175"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=27056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=27056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=27056"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=27056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}