{"id":5271,"date":"2021-03-23T16:33:53","date_gmt":"2021-03-23T16:33:53","guid":{"rendered":"https:\/\/ded9.com\/?p=5271"},"modified":"2025-12-09T10:56:52","modified_gmt":"2025-12-09T10:56:52","slug":"java-access-level-controllers-training-in-plain-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/java-access-level-controllers-training-in-plain-language\/","title":{"rendered":"What Is Java Access Level Controllers \u2014 Understanding Java\u2019s Access Modifiers"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn about the different types of\u00a0<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/Access_modifiers\" target=\"_blank\" rel=\"noopener\"><span style=\"font-size: 12pt;\">Java access Modifiers<\/span><\/a><\/span> and how they work in various scenarios.<\/span><span id=\"more-50131\"><\/span><\/p>\n<h2><span style=\"font-size: 18pt;\">What is an Access Modifier?<\/span><\/h2>\n<p>Access level controllers are keywords that determine the accessibility (access rate) of a class, interface, variable, data member, method, or constructor.\u00a0They are also known as visibility modifiers.<\/p>\n<p>You can encapsulate the code using access level adjusters. This means specifying what part of a program can access class members. Therefore, data misuse can be prevented.<\/p>\n<h2>Types of access level adjusters- Access Modifiers in Java<\/h2>\n<h3>Access level adjusters inside the package<\/h3>\n<p>A package container collects different and related types (Java classes, interfaces, counting, and annotations).<\/p>\n<p>Four keywords regulate access levels in <a href=\"https:\/\/ded9.com\/what-is-object-orientation-in-java-and-how-to-use-it\/\">Java<\/a>:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-5272 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Access-Modifiers-in-Java.jpg\" alt=\"Four keywords regulate access levels in Java:\" width=\"652\" height=\"212\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Access-Modifiers-in-Java.jpg 652w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Access-Modifiers-in-Java-300x98.jpg 300w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/p>\n<p>All access level regulators are described in detail below.<\/p>\n<h3><span style=\"font-size: 14pt;\">1. Default access level<\/span><\/h3>\n<p>If the access level regulator is not explicitly specified for a class, variable, method, or constructor, the access level defaults to the default.<\/p>\n<p>Example 1: Define the default access level<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">package defaultPackage;<\/li>\n<li dir=\"ltr\">class Logger {<\/li>\n<li dir=\"ltr\">void message () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cThis is a message\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Here, the Logger class has a default access level and is available to classes in the defaultPackage package. You will get an error if you import the Logger class in different packages and try to use it.<\/p>\n<h3><span style=\"font-size: 14pt;\">2- Private access level<\/span><\/h3>\n<p>Only data members and methods can be declared private; classes or interfaces cannot. However, internal classes in the nested class can be declared private.<\/p>\n<p>You can access private variables outside the class if the class has public receiver methods.<\/p>\n<p><strong>Example 2: Define a private access level<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public class Data {<\/li>\n<li dir=\"ltr\">private String name;<\/li>\n<li dir=\"ltr\">public String getName () {<\/li>\n<li dir=\"ltr\">return this.name;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void setName (String name) {<\/li>\n<li dir=\"ltr\">this.format = name;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public class Main {<\/li>\n<li dir=\"ltr\">Public static void main (String [] main) {<\/li>\n<li dir=\"ltr\">Data d = new Data ();<\/li>\n<li dir=\"ltr\">d.setName (&#8220;Programming&#8221;);<\/li>\n<li dir=\"ltr\">System.out.println (d.getName ());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Programiz<\/p>\n<\/blockquote>\n<p>Here, name is a private variable and is only accessible within the Data class.\u00a0However, with the help of public receiver methods, it can be accessed in the Main class.<\/p>\n<h3><span style=\"font-size: 14pt;\">3- Access level protected<\/span><\/h3>\n<p>The protected access level provides access to the same package and classes that are direct subclasses of your base class. Data members and methods can be protected, whereas classes or interfaces cannot.<\/p>\n<p><strong>Example 3: Define a protected access level<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Logger.java<\/li>\n<li dir=\"ltr\">package package1;<\/li>\n<li dir=\"ltr\">public class Logger {<\/li>\n<li dir=\"ltr\">protected void debug (String logLine) {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;Debug line:&#8221; + logLine);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">\/\/ Main.java<\/li>\n<li dir=\"ltr\">package package2;<\/li>\n<li dir=\"ltr\">import package1.Logger;<\/li>\n<li dir=\"ltr\">public class Main extends Logger {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Main logger = new Main ();<\/li>\n<li dir=\"ltr\">\/\/ invokes debug () from Logger class<\/li>\n<li dir=\"ltr\">logger.debug (\u201chello from main\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Debug line: hello from main<\/p>\n<\/blockquote>\n<p>Logger.java and Main.java are in different packages. The debug() method in the Logger class is protected and accessible only within the package. However, it is also available in the Main class because Main inherits from Logger.<\/p>\n<h3><span style=\"font-size: 14pt;\">4- Public access level<\/span><\/h3>\n<p>The unlimited public access level applies to classes and interfaces, including methods, data members, and variables.<\/p>\n<p><strong>Example 4: Define the public access level<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ Logger.java<\/li>\n<li dir=\"ltr\">public class Logger {<\/li>\n<li dir=\"ltr\">public int debugLevel = 1;<\/li>\n<li dir=\"ltr\">public void debug (String logLine) {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;Debug:&#8221; + logLine);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void info (String logLine) {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;Info:&#8221; + logLine);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">\/\/ LoggerImp.java<\/li>\n<li dir=\"ltr\">public class LoggerImp {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Logger logger = new Logger ();<\/li>\n<li dir=\"ltr\">logger.debug (\u201cdebug with level\u201d + logger.debugLevel);<\/li>\n<li dir=\"ltr\">logger.debugLevel = 5;<\/li>\n<li dir=\"ltr\">logger.info (\u201cinfo with level\u201d + logger.debugLevel);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Debug: debug with level 1<\/p>\n<p dir=\"ltr\">Info: info with level 5<\/p>\n<\/blockquote>\n<p>Here, in the LoggerImp class, you were able to create a Logger instance because Logger is public. Variables and methods within the LoggerImp class are also public. Hence, you can use it directly in the LoggerImp class.<\/p>\n<p><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50133\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-java-access-modifiers-public-p.jpeg\" alt=\"Here, in the LoggerImp class, you were able to create a Logger instance because Logger is public. Variables and methods within the LoggerImp class are also public. Hence, you can use it directly in the LoggerImp class.\" width=\"512\" height=\"512\" data-ll-status=\"loaded\" \/><\/p>\n<p>The <code>TestE<\/code> class, base subclass, allows access to the protected members <code>TestB<\/code> through inheritance. However, invoking a protected superclass method directly will result in a compile-time error.<\/p>\n<p>Java access modifiers are straightforward once you grasp the basics. It&#8217;s important not to mix up default and protected access levels. A helpful tip: default access is more restrictive than protected access, because protected members can be accessed within subclasses.<\/p>\n<p style=\"text-align: start;\"><button type=\"button\" data-url=\"https:\/\/www.youtube.com\/watch?v=QKjnbC3UBtY\"><span style=\"color: #333333; font-family: Georgia, Times New Roman, Bitstream Charter, Times, serif;\"><span style=\"font-size: 16px; background-color: #ffffff;\">I recently created a video on Java access modifiers that provides a detailed explanation. You can check it out on YouTube here:<\/span><\/span>Java Access Modifiers Explained<\/button>.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are access modifiers (access level controllers) in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>They are keywords that define who can access a class, method, or variable \u2014 controlling visibility across the application.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Which access levels exist in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Java supports four access levels: public, protected, package-private (default, no modifier), and private.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">When should I choose each access level?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use private to hide implementation details within a class; use default (package-private) to restrict access to the same package; use protected when subclasses should access members; and use public for API-level methods or classes accessible from anywhere.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the different types of\u00a0Java access Modifiers and how they work in various scenarios. What is an Access Modifier? Access level controllers are keywords that determine the accessibility (access rate) of a class, interface, variable, data member, method, or constructor.\u00a0They are also known as visibility modifiers. You can encapsulate [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5272,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5271"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5271\/revisions"}],"predecessor-version":[{"id":265973,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5271\/revisions\/265973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5272"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}