{"id":10814,"date":"2025-08-29T09:43:49","date_gmt":"2025-08-29T09:43:49","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=10814"},"modified":"2025-08-29T09:43:49","modified_gmt":"2025-08-29T09:43:49","slug":"access-modifiers-in-java-2025-guide","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/access-modifiers-in-java-2025-guide\/","title":{"rendered":"Access Modifiers in Java \u2013 Complete Guide with Examples"},"content":{"rendered":"<p>When you first start learning Java, one of the first concepts you will encounter is <strong>Access Modifiers in <a href=\"https:\/\/internship.kaashivinfotech.com\/java-internship\/\">Java<\/a><\/strong>. They control the visibility, accessibility and scope of <strong>classes<\/strong>, <strong>variables<\/strong>, <strong>methods<\/strong> and <strong>constructors<\/strong>. Put simply, access modifiers determine who can access what in your program.<\/p>\n<p>Without access modifiers, a Java program could be chaotic. Anyone could access and change anything. It is kind of like locking your house without a key! Access modifiers determine who has access to the objects you create.<\/p>\n<h2><strong>\ud83d\udd11<\/strong><strong> What are Access Modifiers in Java?<\/strong><\/h2>\n<p><a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-modifiers\" target=\"_blank\" rel=\"noopener\"><strong>Access modifiers<\/strong> in Java<\/a> are <strong>keywords<\/strong> used to specify the accessibility of classes, methods, constructors and variables. They play an important role in <strong>encapsulation<\/strong>, that is a <strong>one of the key concepts of Object Oriented Programming<\/strong>, because it limits the accessibility of class members.<\/p>\n<p>In Java, there are <strong>four main types of access modifiers<\/strong>:<\/p>\n<ol>\n<li><strong>Public<\/strong><\/li>\n<li><strong>Private<\/strong><\/li>\n<li><strong>Protected<\/strong><\/li>\n<li><strong>Default (no modifier)<\/strong><\/li>\n<\/ol>\n<h2><strong>\ud83d\udccc<\/strong><strong> Types of Access Modifiers in Java<\/strong><\/h2>\n<figure id=\"attachment_10817\" aria-describedby=\"caption-attachment-10817\" style=\"width: 768px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-10817\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Types-of-Access-Modifiers-in-Java.webp\" alt=\"\" width=\"768\" height=\"290\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Types-of-Access-Modifiers-in-Java.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Types-of-Access-Modifiers-in-Java-300x113.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Types-of-Access-Modifiers-in-Java-380x143.webp 380w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><figcaption id=\"caption-attachment-10817\" class=\"wp-caption-text\">Types of Access Modifiers in Java<\/figcaption><\/figure>\n<p>Let\u2019s break down each type of access modifier with examples and real-life analogies<\/p>\n<h3><strong>Public Access Modifier<\/strong><\/h3>\n<ul>\n<li><strong>Keyword:<\/strong> public<\/li>\n<li><strong>Scope:<\/strong> Accessible from anywhere in the project.<\/li>\n<li><strong>Analogy:<\/strong> Think of a public park \u2013 anyone can enter.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public class Student {\r\n\r\n\u00a0\u00a0\u00a0 public String name = \"John\";\r\n\r\n\u00a0\u00a0\u00a0 public void showName() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Name: \" + name);\r\n\r\n\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p>Here, both name and showName() can be accessed from any class.<\/p>\n<h3><strong>Private Access Modifier<\/strong><\/h3>\n<ul>\n<li><strong>Keyword:<\/strong> private<\/li>\n<li><strong>Scope:<\/strong> Accessible only within the same class.<\/li>\n<li><strong>Analogy:<\/strong> Your ATM PIN \u2013 only you should know it, not even your best friend.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class BankAccount {\r\n\r\n\u00a0\u00a0\u00a0 private double balance = 1000;\r\n\r\n\r\n\r\n\r\n\u00a0\u00a0\u00a0 private void showBalance() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Balance: \" + balance);\r\n\r\n\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<p>Here, balance and showBalance() are hidden from outside classes.<\/p>\n<h3><strong>Protected Access Modifier<\/strong><\/h3>\n<ul>\n<li><strong>Keyword:<\/strong> protected<\/li>\n<li><strong>Scope:<\/strong> Accessible within the same package and also by subclasses in different packages.<\/li>\n<li><strong>Analogy:<\/strong> Think of a family recipe \u2013 it\u2019s shared among relatives (subclasses) but not with strangers.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Vehicle {\r\n\r\n\u00a0\u00a0\u00a0 protected String type = \"Car\";\r\n\r\n}\r\n\r\nclass Car extends Vehicle {\r\n\r\n\u00a0\u00a0\u00a0 public void display() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Vehicle type: \" + type);\r\n\r\n\u00a0\u00a0\u00a0 }\r\n\r\n}<\/pre>\n<h3><strong>Default Access Modifier (No Keyword)<\/strong><\/h3>\n<ul>\n<li><strong>Keyword:<\/strong> none (just leave it blank)<\/li>\n<li><strong>Scope:<\/strong> Accessible only within the same package.<\/li>\n<li><strong>Analogy:<\/strong> A neighborhood community park \u2013 only locals can enter.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Teacher {\r\n\r\n\u00a0\u00a0\u00a0 String subject = \"Math\";\u00a0 \/\/ default access\r\n\r\n}<\/pre>\n<h2><strong>\ud83d\udcca<\/strong><strong> Quick Comparison Table \u2013 Types of Access Modifiers in Java<\/strong><\/h2>\n<table style=\"height: 321px;\" width=\"701\">\n<thead>\n<tr>\n<td>\n<p style=\"text-align: center;\"><strong>Modifier<\/strong><\/p>\n<\/td>\n<td style=\"text-align: center;\"><strong>Within Class<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Same Package<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Subclass (diff package)<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Other Packages<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n<p style=\"text-align: center;\"><strong>Public<\/strong><\/p>\n<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<\/tr>\n<tr>\n<td>\n<p style=\"text-align: center;\"><strong>Private<\/strong><\/p>\n<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u274c<\/td>\n<td style=\"text-align: center;\">\u274c<\/td>\n<td>\n<p style=\"text-align: center;\">\u274c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>Protected<\/strong><\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td>\n<p style=\"text-align: center;\">\u274c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>Default<\/strong><\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u2705<\/td>\n<td style=\"text-align: center;\">\u274c<\/td>\n<td>\n<p style=\"text-align: center;\">\u274c<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>\ud83d\udd27<\/strong><strong> Non Access Modifiers in Java<\/strong><\/h2>\n<figure id=\"attachment_10818\" aria-describedby=\"caption-attachment-10818\" style=\"width: 1451px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-10818\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java.webp\" alt=\"\" width=\"1451\" height=\"435\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java.webp 1451w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java-300x90.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java-1024x307.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java-768x230.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java-380x114.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java-800x240.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Non-Access-Modifiers-in-Java-1160x348.webp 1160w\" sizes=\"(max-width: 1451px) 100vw, 1451px\" \/><figcaption id=\"caption-attachment-10818\" class=\"wp-caption-text\">Non Access Modifiers in Java<\/figcaption><\/figure>\n<p>In addition to access modifiers, Java has <strong>non access modifiers<\/strong> that define other properties of a methods, classes or variables. Non access modifiers do not deal with visibility but control <strong>behavior<\/strong>.<\/p>\n<p><strong>Common Non Access Modifiers in Java:<\/strong><\/p>\n<ol>\n<li><strong>static<\/strong> \u2013 Belongs to the class, not the object.<\/li>\n<li><strong>final<\/strong> \u2013 Used to declare constants or prevent inheritance\/overriding.<\/li>\n<li><strong>abstract<\/strong> \u2013 For abstract classes and methods.<\/li>\n<li><strong>synchronized<\/strong> \u2013 Controls thread access.<\/li>\n<li><strong>volatile<\/strong> \u2013 For variables modified by multiple threads.<\/li>\n<\/ol>\n<p>\ud83d\udc49 Example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Demo {\r\n\r\n\u00a0\u00a0\u00a0 static int count = 0;\u00a0 \/\/ static variable\r\n\r\n\u00a0\u00a0\u00a0 final int MAX = 100;\u00a0\u00a0 \/\/ final variable\r\n\r\n}<\/pre>\n<p>Including <strong>non access modifiers in Java<\/strong> makes your program more efficient and structured.<\/p>\n<h2><strong>\ud83d\udca1<\/strong><strong> Why Use Access Modifiers in Java?<\/strong><\/h2>\n<ul>\n<li>Helps in data hiding and security<\/li>\n<li>Provides encapsulation<\/li>\n<li>Controls variables and methods scope<\/li>\n<li>Improves code maintainability<\/li>\n<li>Helps in reusability and inheritance<\/li>\n<\/ul>\n<h2><strong>\ud83c\udfaf<\/strong><strong> Real-Life Example of Access Modifiers<\/strong><\/h2>\n<figure id=\"attachment_10819\" aria-describedby=\"caption-attachment-10819\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\" wp-image-10819\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers.webp\" alt=\"\" width=\"636\" height=\"424\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers.webp 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers-300x200.webp 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers-1024x683.webp 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers-768x512.webp 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers-380x253.webp 380w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers-800x533.webp 800w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/08\/Real-Life-Example-of-Access-Modifiers-1160x773.webp 1160w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><figcaption id=\"caption-attachment-10819\" class=\"wp-caption-text\">Real-Life Example of Access Modifiers<\/figcaption><\/figure>\n<p>Think of a <strong>company<\/strong>:<\/p>\n<ul>\n<li><strong>Public:<\/strong> Reception area \u2013 anyone can enter.<\/li>\n<li><strong>Private:<\/strong> CEO\u2019s cabin \u2013 only the CEO can access.<\/li>\n<li><strong>Protected:<\/strong> HR files \u2013 employees of the company can access, but outsiders cannot.<\/li>\n<li><strong>Default:<\/strong> Internal office discussions \u2013 limited to staff within the same branch.<\/li>\n<\/ul>\n<h2><strong>\ud83e\udd14<\/strong><strong> Access Modifiers vs Non Access Modifiers in Java<\/strong><\/h2>\n<table style=\"height: 285px;\" width=\"695\">\n<thead>\n<tr>\n<td>\n<p style=\"text-align: center;\"><strong>Feature<\/strong><\/p>\n<\/td>\n<td style=\"text-align: center;\"><strong>Access Modifiers<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Non Access Modifiers<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n<p style=\"text-align: center;\"><strong>Purpose<\/strong><\/p>\n<\/td>\n<td style=\"text-align: center;\">Control visibility<\/td>\n<td style=\"text-align: center;\">Control behavior<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>Examples<\/strong><\/td>\n<td style=\"text-align: center;\">public, private, protected, default<\/td>\n<td>\n<p style=\"text-align: center;\">static, final, abstract, synchronized<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>Focus<\/strong><\/td>\n<td style=\"text-align: center;\">Security &amp; encapsulation<\/td>\n<td>\n<p style=\"text-align: center;\">Performance &amp; functionality<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>\ud83d\udcdd<\/strong><strong> Interview Questions on Access Modifiers in Java<\/strong><\/h2>\n<ol>\n<li>What are the types of access modifiers in Java?<\/li>\n<li>Difference between default and protected modifier?<\/li>\n<li>Can we use private constructor in Java?<\/li>\n<li>Difference between access modifiers and non access modifiers in Java?<\/li>\n<li>Can abstract methods be private?<\/li>\n<\/ol>\n<h2><strong>\u2705<\/strong><strong> Conclusion<\/strong><\/h2>\n<p>In this guide, we discussed <strong>Access Modifiers in Java<\/strong>, their type (<strong>public<\/strong>, <strong>private<\/strong>, <strong>protected<\/strong>, <strong>default<\/strong>), and <strong>non access modifiers in Java<\/strong> using examples, tables, and real-world analogies.<\/p>\n<p>To recap:<\/p>\n<ul>\n<li>Use access modifiers to control who can access what.<\/li>\n<li>Use non access modifiers to define how elements behave.<\/li>\n<\/ul>\n<p>Once you get the hang of these you will have <strong>secure<\/strong>, <strong>modular<\/strong> and <strong>professional Java code<\/strong>.<\/p>\n<h2>Related Reads<\/h2>\n<ul>\n<li style=\"text-align: left;\"><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/java-data-types-guide-2025\/\">Java Data Types \u2013 Complete Guide to Primitive and Non-Primitive Types<\/a><\/li>\n<li style=\"text-align: left;\"><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/where-is-java-used-in-2025\/\">Where is Java Used in 2025? (10 Real-World Java Programming Applications<\/a><\/li>\n<li style=\"text-align: left;\"><a href=\"https:\/\/www.wikitechy.com\/operators-in-java\/\" target=\"_blank\" rel=\"noopener\">Mastering Java Programming Operators \u2013 With Real Examples<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>When you first start learning Java, one of the first concepts you will encounter is Access Modifiers in Java. They control the visibility, accessibility and scope of classes, variables, methods and constructors. Put simply, access modifiers determine who can access what in your program. Without access modifiers, a Java program could be chaotic. Anyone could [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":10816,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3356],"tags":[8708,8711,8713,781,8714,8712,8612,8329,8710,8709],"class_list":["post-10814","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-access-modifiers-in-java","tag-java-access-specifiers","tag-java-encapsulation","tag-java-interview-questions","tag-java-keywords","tag-java-modifiers-examples","tag-java-programming-2025","tag-java-tutorial","tag-non-access-modifiers-in-java","tag-types-of-access-modifiers-in-java"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=10814"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/10814\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/10816"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=10814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=10814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=10814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}