User Tools

Site Tools


java:access_specifiers_accessors_and_mutators

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
java:access_specifiers_accessors_and_mutators [2020/09/12 01:27] – created mithatjava:access_specifiers_accessors_and_mutators [2020/09/12 02:01] (current) – [this] mithat
Line 1: Line 1:
-======  Access Specifiers, Accessors, and Mutators ====== +======  Access Modifiers, Accessors, and Mutators ====== 
  
-===== Access specifiers ===== +===== Access modifiers ===== 
-The ''ClickerCounter'' class definition we used previously encapsulates the state and behavior of a clicker counter as an object should. But the definition has issues. For example, if we change our example as follows:+The ''ClickerCounter'' class definition we used previously encapsulates the state and behavior of a clicker counter as an object should. 
 + 
 +<file java ClickerCounter.java> 
 +public class ClickerCounter { 
 +     
 +    int count; 
 +     
 +    void click(){ 
 +        count++; 
 +    } 
 +     
 +    void reset(){ 
 +        count = 0; 
 +    }     
 +
 +</file> 
 + 
 +But the definition has issues. For example, if we change our ''main'' code as follows:
  
 <file java ClickerExample.java> <file java ClickerExample.java>
Line 20: Line 37:
 </file> </file>
  
-the object will happily oblige the user's wish to set the count to some arbitrary value. That's not something you can typically do with a clicker counter. In other words, the object offers no **protection** of its state. This is easy enough to solve with Java'access modifiers. Let's look at a modified version of our class definition that gives us some protection using access modifiers:+the object will happily oblige the user's wish to set the count to some arbitrary value. That's not something you can typically do with a clicker counter and probably not what you want from your ''ClickerCounter'' objectWhat we are seeing here is that the object offers no **protection** of its state. 
 + 
 +We can add protection of members using **access modifiers**. Let's look at a modified version of our class definition that gives us some protection by using two of Java'access modifiers:
  
 <file java ClickerCounter.java> <file java ClickerCounter.java>
Line 37: Line 56:
 </file> </file>
  
-Access modifiers are placed before the start of a member. Java offers four levels of protection, and they just so happen to all start with the letter 'p':+Access modifiers are placed before the start of a member definition. Java offers four levels of protection, and they just so happen to all start with the letter 'p':
   * ''package'': this is what you get by default (i.e., when you don't specify anything, as in our first definition of the class). It means anything within the same package as the class definition is able to access the member.   * ''package'': this is what you get by default (i.e., when you don't specify anything, as in our first definition of the class). It means anything within the same package as the class definition is able to access the member.
   * ''public'': anyone anywhere can access the member. This is a good choice for methods that are part of the class' interface.   * ''public'': anyone anywhere can access the member. This is a good choice for methods that are part of the class' interface.
Line 43: Line 62:
   * ''protected'': only those of the same _or a derived_ class can access the member. ((The difference between ''private'' and ''protected'' won't be clear until you've studied class inheritance.))   * ''protected'': only those of the same _or a derived_ class can access the member. ((The difference between ''private'' and ''protected'' won't be clear until you've studied class inheritance.))
  
-With the changes made above, if we try to run ''ClickerExample'' we will see that the code will refuse to compile. When the compiler hits the statement+If we try to compile ''ClickerExample''  with the changes made above, we will see that the code will refuse to compile. When the compiler hits the statement:
  
 <code java> <code java>
Line 49: Line 68:
 </code> </code>
  
-it cannot access the ''count'' member of ''myCounter'' because we said it's private. Remember, we want our objects to be **self-governing**, and this helps make sure this is the case.+it cannot access the ''count'' member of ''myCounter'' because we said it's private.
  
 <WRAP center round tip 80%> <WRAP center round tip 80%>
-In Java programming, it's considered a best practice to make all member variables ''private'' or ''protected''except in some rare situations+It's considered a best practice in Java programming to make all member variables ''private'' or ''protected'' except in some rare situations.
- +
-Methods can be at whatever access level is appropriate for their use. If it is part of the class' interface, it should be ''public''. If it's for use only internally by the object, it will typically be ''private'' or ''protected''.+
 </WRAP> </WRAP>
 +
 +We want objects to be **self-governing**, and the proper use of access specifiers helps ensure this is the case. Methods can be at whatever access level is appropriate for their use. If the method is part of the class' interface, it should be ''public''. If it's for use only internally by the object, it will typically be ''private'' or ''protected''.
 +
 +
  
  
Line 214: Line 235:
 ===== this ===== ===== this =====
  
-FIXME+Every class has an automatically generated ''this'' member variable that points to the object it's associated with. In other words, it's a reference back to the object itself. 
 + 
 +''this'' can be used to simplify writing mutators and other methods that have parameters because it eliminates the need to come up with new names for parameters. Let's see how this works by modifying the ''setMaxCount()'' method. 
 + 
 + 
 +<code java> 
 +public void setMaxCount(int maxCount) { 
 +    if (maxCount > 0) { 
 +        this.maxCount = maxCount; 
 +    } else { 
 +        System.out.println("Invalid maximum count: " + maxCount); 
 +    } 
 +
 +</code> 
 + 
 +We have changed the name of the parameter to ''maxCount''. Inside the method it shadows the member variable ''maxCount'', but we can still access the member variable through the ''this'' reference. In essence, what we are saying in the above is, "Set this object's maxCount to the maxCount we passed in."
  
 +''this'' will be used in other ways as well, so get comfortable with it. Think of it as meaning, "this object".
  
-Copyright © 2011-2018 Mithat Konar. All rights reserved.+Copyright © 2020 Mithat Konar. All rights reserved.
java/access_specifiers_accessors_and_mutators.1599874056.txt.gz · Last modified: 2020/09/12 01:27 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki