My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the getters/setters of a class within the class?
So for instance, if I have the following class:
public class Person { private String name; private int age; }
and I want to write a toString() method for it. Can I just write:
public String toString() { return name + " " + age; }
OR do I need to do something like this:
public String toString() { return this.getName() + " " + this.getAge(); }
Yes, the methods of your class should call the getters and setters. The whole point in writing getters and setters is future proofing. You could make every property a field and directly expose the data to the users of the class.
The Java coding convention states that methods (getters and setters are methods) should be after constructors declarations. It just a convention and it exists to make code easier to read in general.
Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.
To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B . You can do this e.g. by passing it as a parameter to a method of B , or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter .
You CAN do either one. However, your professor might appreciate using the methods instead of the direct access. Here's why.
Let's say you have a class like this:
class SomeClass { private int someValue; private String someString; public SomeClass(int someValue, String someString) { this.someValue = someValue; this.someString = someString; } public int someValue() { return this.someValue; } public String someString() { return this.someString; } public String toString() { return someValue + ": " + someString; } }
It's pretty straightforward, right? Well, what if all of a sudden we want to CHANGE the implementation of how we calculate someValue
, and base it off of someString
:
public int someValue() { int value = 0; for(int i = 0; i < someString.length; i++) { if(someString.charAt(i) == ' ') value++; } return value; }
Now you also have to change every place where variable someValue
was used.
So if you want to make the code easier to maintain in the long run, use the methods calls. This way when you code changes on you (and trust me, it changes all the time) you only have to change it in one spot instead of two.
And yes, you would want to use a method call in getting someString
instead of the direct access in the last method :-)
When I design a class I try to make a clear distinction between the inside (implementation details) and the outside (the interface exposed to the world). Getters and setters provide a convenient place to convert values between the form in which they are stored in the object’s instance members and the form in which the outside world sees them. Using getters and setters internally would muck that up, because they'd be getting used by both the inside and outside.
If you find yourself wanting to hide part of a class from another part of the same class, consider breaking off the part you want to hide into its own class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With