{"id":17,"date":"2015-12-19T06:28:38","date_gmt":"2015-12-19T06:28:38","guid":{"rendered":"http:\/\/www.kenwalger.com\/blog\/?p=17"},"modified":"2017-07-09T22:06:43","modified_gmt":"2017-07-10T05:06:43","slug":"java-getters-setters","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/java\/java-getters-setters\/","title":{"rendered":"Java getters and setters"},"content":{"rendered":"<p><code>getters<\/code> and <code>setters<\/code> come out of one of the fundamental Object Oriented Programming (OOP) concepts called <strong>encapsulation<\/strong>. Encapsulation is a way of wrapping data (variables) and the coding doing something with that data (methods) together. We encapsulate class variables and hide them from other classes and make them accessible only through methods of the current class.<\/p>\n<h3>Encapsulation with Getters and Setters<\/h3>\n<p>To achieve encapsulation in Java<\/p>\n<ul>\n<li>Declare the variables of a class as private.<\/li>\n<li>Provide public <code>setter<\/code> and <code>getter<\/code> methods to modify and view the variables values.<\/li>\n<\/ul>\n<p>That sounds all great and, if you are anything like me, doesn&#8217;t really mean much without an example. So, let&#8217;s do that.<\/p>\n<div class=\"code-attachment-filename\" data-behavior=\"file\">Employee.java<\/div>\n<div class=\"highlight highlight-java\">\n<pre><span class=\"kd\">public<\/span> <span class=\"kd\">class<\/span> <span class=\"nc\">Employee<\/span><span class=\"o\">{<\/span>\n\n   <span class=\"kd\">private<\/span> <span class=\"n\">String<\/span> <span class=\"n\">name<\/span><span class=\"o\">;<\/span>\n   <span class=\"kd\">private<\/span> <span class=\"n\">String<\/span> <span class=\"n\">idNum<\/span><span class=\"o\">;<\/span>\n   <span class=\"kd\">private<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">age<\/span><span class=\"o\">;<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"kt\">int<\/span> <span class=\"nf\">getAge<\/span><span class=\"o\">(){<\/span>\n      <span class=\"k\">return<\/span> <span class=\"n\">age<\/span><span class=\"o\">;<\/span>\n   <span class=\"o\">}<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getName<\/span><span class=\"o\">(){<\/span>\n      <span class=\"k\">return<\/span> <span class=\"n\">name<\/span><span class=\"o\">;<\/span>\n   <span class=\"o\">}<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"n\">String<\/span> <span class=\"nf\">getIdNum<\/span><span class=\"o\">(){<\/span>\n      <span class=\"k\">return<\/span> <span class=\"n\">idNum<\/span><span class=\"o\">;<\/span>\n   <span class=\"o\">}<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">setAge<\/span><span class=\"o\">(<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">newAge<\/span><span class=\"o\">){<\/span>\n      <span class=\"n\">age<\/span> <span class=\"o\">=<\/span> <span class=\"n\">newAge<\/span><span class=\"o\">;<\/span>\n   <span class=\"o\">}<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">setName<\/span><span class=\"o\">(<\/span><span class=\"n\">String<\/span> <span class=\"n\">newName<\/span><span class=\"o\">){<\/span>\n      <span class=\"n\">name<\/span> <span class=\"o\">=<\/span> <span class=\"n\">newName<\/span><span class=\"o\">;<\/span>\n   <span class=\"o\">}<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">setIdNum<\/span><span class=\"o\">(<\/span> <span class=\"n\">String<\/span> <span class=\"n\">newId<\/span><span class=\"o\">){<\/span>\n      <span class=\"n\">idNum<\/span> <span class=\"o\">=<\/span> <span class=\"n\">newId<\/span><span class=\"o\">;<\/span>\n   <span class=\"o\">}<\/span>\n<span class=\"o\">}<\/span>\n<\/pre>\n<\/div>\n<p>Obviously, we would store much more about an <code>employee<\/code>, but this is just a sample. Now, to the question of how do you access this &#8220;hidden&#8221; data from another class.<\/p>\n<div class=\"code-attachment-filename\" data-behavior=\"file\">RunEmployee.java<\/div>\n<div class=\"highlight highlight-java\">\n<pre><span class=\"kd\">public<\/span> <span class=\"kd\">class<\/span> <span class=\"nc\">RunEmployee<\/span><span class=\"o\">{<\/span>\n\n   <span class=\"kd\">public<\/span> <span class=\"kd\">static<\/span> <span class=\"kt\">void<\/span> <span class=\"nf\">main<\/span><span class=\"o\">(<\/span><span class=\"n\">String<\/span> <span class=\"n\">args<\/span><span class=\"o\">[]){<\/span>\n      <span class=\"n\">Employee<\/span> <span class=\"n\">employee<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"nf\">Employee<\/span><span class=\"o\">();<\/span>\n      <span class=\"n\">employee<\/span><span class=\"o\">.<\/span><span class=\"na\">setName<\/span><span class=\"o\">(<\/span><span class=\"s\">\"James\"<\/span><span class=\"o\">);<\/span>\n      <span class=\"n\">employee<\/span><span class=\"o\">.<\/span><span class=\"na\">setAge<\/span><span class=\"o\">(<\/span><span class=\"mi\">20<\/span><span class=\"o\">);<\/span>\n      <span class=\"n\">employee<\/span><span class=\"o\">.<\/span><span class=\"na\">setIdNum<\/span><span class=\"o\">(<\/span><span class=\"s\">\"12343ms\"<\/span><span class=\"o\">);<\/span>\n\n      <span class=\"n\">System<\/span><span class=\"o\">.<\/span><span class=\"na\">out<\/span><span class=\"o\">.<\/span><span class=\"na\">print<\/span><span class=\"o\">(<\/span><span class=\"s\">\"Name: \"<\/span> <span class=\"o\">+<\/span> <span class=\"n\">employee<\/span><span class=\"o\">.<\/span><span class=\"na\">getName<\/span><span class=\"o\">()<\/span> <span class=\"o\">+<\/span>  \n                                   <span class=\"s\">\" | Age: \"<\/span> <span class=\"o\">+<\/span> <span class=\"n\">employee<\/span><span class=\"o\">.<\/span><span class=\"na\">getAge<\/span><span class=\"o\">()<\/span> <span class=\"o\">+<\/span>  \n                                   <span class=\"s\">\" | Id: \"<\/span> <span class=\"o\">+<\/span> <span class=\"n\">employee<\/span><span class=\"o\">.<\/span><span class=\"na\">getIdNum<\/span><span class=\"o\">());<\/span>\n    <span class=\"o\">}<\/span>\n<span class=\"o\">}<\/span>\n<\/pre>\n<\/div>\n<p>If we were to run this we would get the following output:<\/p>\n<div class=\"highlight highlight-java\">\n<pre><span class=\"nl\">Name:<\/span> <span class=\"n\">James<\/span> <span class=\"o\">|<\/span> <span class=\"nl\">Age:<\/span> <span class=\"mi\">20<\/span> <span class=\"o\">|<\/span> <span class=\"nl\">Id:<\/span> <span class=\"mi\">12343<\/span><span class=\"n\">ms<\/span>\n<\/pre>\n<\/div>\n<h3>Encapsulation Benefits<\/h3>\n<p>Some of the benefits of encapsulation include:<\/p>\n<ul>\n<li>The fields of a class can be made read-only or write-only.<\/li>\n<li>A class can have total control over what is stored in its fields.<\/li>\n<li>The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.<\/li>\n<\/ul>\n<p>Happy coding!<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F17&#038;t=Java%20getters%20and%20setters&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F17&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2015%2F12%2Fjava-get-set-e1495635972291.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=Java%20getters%20and%20setters\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F17&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F17&#038;title=Java%20getters%20and%20setters\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F17&#038;title=Java%20getters%20and%20setters\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Java%20getters%20and%20setters&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F17\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>getters and setters come out of one of the fundamental Object Oriented Programming (OOP) concepts called encapsulation. Encapsulation is a way of wrapping data (variables) and the coding doing something with that data (methods) together. We encapsulate class variables and hide them from other classes and make them accessible only through methods of the current &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/java\/java-getters-setters\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Java getters and setters&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":327,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[14,15],"yst_prominent_words":[208,207,99,212,222,218,205,217,216,221,219,214,210,206,211,209,204,215,220,213],"class_list":["post-17","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-encapsulation","tag-oop","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2015\/12\/java-get-set-e1495635972291.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-h","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":5,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":441,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/327"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=17"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}