{"id":653,"date":"2018-06-10T00:32:25","date_gmt":"2018-06-10T00:32:25","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=653"},"modified":"2018-06-10T18:51:41","modified_gmt":"2018-06-10T18:51:41","slug":"tips-tricks-only-fields-are-private-by-default","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/tips-tricks-only-fields-are-private-by-default\/","title":{"rendered":"Tips &#038; Tricks: Only Fields are &#8216;private&#8217; by Default"},"content":{"rendered":"<p>Programmers often complain about the verbosity of Java. Once you specify all the modifiers that must be applied, it&#8217;s not difficult to see how it can quickly become verbose:<\/p>\n<pre class=\"brush:jspp\">\r\npublic static void veryLongNamingConventions() {\r\n    \/\/ ...\r\n}\r\n<\/pre>\n<p>JS++ does this differently. Following the OOP principle of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Encapsulation_(computer_programming)\" rel=\"noopener\" target=\"_blank\">encapsulation<\/a>, JS++ provides convenient default rules for <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Modifiers\/Access-Modifiers\" rel=\"noopener\" target=\"_blank\">access modifiers<\/a>.<\/p>\n<p>By default, only fields (variable members of classes) are <code><a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Modifiers\/Access-Modifiers\/private\" rel=\"noopener\" target=\"_blank\">private<\/a><\/code>. All other class members &#8211; such as methods, getters, setters, and constructors &#8211; are <code><a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Modifiers\/Access-Modifiers\/public\" rel=\"noopener\" target=\"_blank\">public<\/a><\/code> by default.<\/p>\n<p>This makes it very easy to write concise code:<\/p>\n<pre class=\"brush:jspp\">\r\nclass Point\r\n{\r\n    int x, y;\r\n\r\n    Point(int x, int y) {\r\n        this.x = x;\r\n        this.y = y;\r\n    }\r\n\r\n    int getX() { return this.x; }\r\n    int getY() { return this.y; }\r\n}\r\n<\/pre>\n<p>In the above code, the fields <code>x<\/code> and <code>y<\/code> are private. Meanwhile, the constructor and the getX\/getY methods are all public. We can be explicit and manually specify the access modifiers, but it&#8217;s not <em>necessary<\/em> in JS++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Programmers often complain about the verbosity of Java. Once you specify all the modifiers that must be applied, it&#8217;s not difficult to see how it can quickly become verbose: public static void veryLongNamingConventions() { \/\/ &#8230; } JS++ does this differently. Following the OOP principle of encapsulation, JS++ provides convenient default rules for access modifiers. &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/tips-tricks-only-fields-are-private-by-default\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tips &#038; Tricks: Only Fields are &#8216;private&#8217; by Default&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/653"}],"collection":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/comments?post=653"}],"version-history":[{"count":4,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":657,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/653\/revisions\/657"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}