{"id":6524,"date":"2024-04-21T23:41:57","date_gmt":"2024-04-22T06:41:57","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-public-private\/"},"modified":"2024-04-21T23:42:43","modified_gmt":"2024-04-22T06:42:43","slug":"java-public-private","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-public-private\/","title":{"rendered":"Java Public Private"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about Java public and private access modifiers, and how to use them to control the access to members of a class effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Java public and private access modifiers<\/h2>\n\n\n\n<p>In Java, access modifiers control the visibility and accessibility of <a href=\"https:\/\/zentut.com\/java-tutorial\/java-class\/\">class<\/a> members including fields and methods. They determine which members can be accessed only within the class and\/or from the outside of the class.<\/p>\n\n\n\n<p>Java has the following access modifiers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>public<\/code><\/li>\n\n\n\n<li><code>private<\/code><\/li>\n\n\n\n<li><code>protected<\/code><\/li>\n<\/ul>\n\n\n\n<p>This tutorial focuses on the first two access modifiers: <code>public<\/code> and <code>private<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">public access modifier<\/h2>\n\n\n\n<p>The <code>public<\/code> access modifier allows a field or method can be accessed from other classes. To declare a member as public, you use the <code>public<\/code> keyword: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span> <\/span>{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">int<\/span> publicField;\n    \n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">publicMethod<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following marks the <code>sayHi()<\/code> method of the <code>Person<\/code> class as public:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span> <\/span>{\n    String name;\n    <span class=\"hljs-keyword\">int<\/span> age;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sayHi<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        System.out.printf(<span class=\"hljs-string\">\"Hi. It's %s.\"<\/span>, <span class=\"hljs-keyword\">this<\/span>.name);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By marking the <code>sayHi()<\/code> method as public, you can call the <code>sayHi()<\/code> method from any class.<\/p>\n\n\n\n<p>In practice, you use the <code>public<\/code> access modifiers when you want a class member to be accessible from any part of your program.<\/p>\n\n\n\n<p>Notice that if a member has no access modifier, it has something called the &#8220;default&#8221; access modifier. In this case, the member will be accessible within the same package in which it is defined. You&#8217;ll learn more about packages in the upcoming tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">private access modifier<\/h2>\n\n\n\n<p>The <code>private<\/code> access modifier restricts access to the members within the same class. It means that private members can be only accessible within the same class. In other words, private members are not visible from other classes<\/p>\n\n\n\n<p>To mark a member as private, you use the <code>private<\/code> keyword: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span> <\/span>{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">int<\/span> privateField;\n   \n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">privateMethod<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following marks the <code>name<\/code> and <code>age<\/code> of the <code>Person<\/code> class as <code>private<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span> <\/span>{\n    <span class=\"hljs-keyword\">private<\/span> String name;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">int<\/span> age;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sayHi<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        System.out.printf(<span class=\"hljs-string\">\"Hi. It's %s.\"<\/span>, <span class=\"hljs-keyword\">this<\/span>.name);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Typically, you use the <code>private<\/code> access modifier when you want to prevent direct access from other classes and promote encapsulation by hiding the implementation details.<\/p>\n\n\n\n<p>In this example, you don&#8217;t want other classes to directly access the <code>name<\/code> and <code>age<\/code> fields. <\/p>\n\n\n\n<p>Instead, you will provide public methods, known as getters\/setters, so that other classes can call them to manipulate these fields. Within these public methods, you can incorporate logic to handle these fields, such as validating before applying changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java public and private guidelines<\/h2>\n\n\n\n<p>Here are the general guidelines for using public and private access modifiers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>public<\/code> methods for intended public API. Minimize the use of public fields to avoid direct access and promote encapsulation.<\/li>\n\n\n\n<li>Use <code>private<\/code> methods for helper functions. Favor encapsulation by making fields private.<\/li>\n\n\n\n<li>Use the least privilege principle when choosing access modifiers, which expose only what is necessary for other classes to use.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java access modifiers control access to members of a class including fields and methods.<\/li>\n\n\n\n<li>Use the <code>public<\/code> access modifier when you want the members of a class to be accessible from any other classes.<\/li>\n\n\n\n<li>Use the <code>private<\/code> access modifier when you want the members of a class to be accessible only within the class they defined.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about Java public and private access modifiers, and how to use them to control the access to members of a class effectively. Introduction to Java public and private access modifiers In Java, access modifiers control the visibility and accessibility of class members including fields and methods. They determine [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6524","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6524"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6524\/revisions"}],"predecessor-version":[{"id":6526,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6524\/revisions\/6526"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}