To prevent tampering, sometimes it's necessary for data fields to be declared as private in a class. If you attempt to access a private data field from a client, a compile error will occur. In the following example, we have a two classes: CircleWithPrivateDataFields.java and TestCircleWithPrivateDataFields.java.
In CircleWithPrivateDataFields.java, the radius and numberOfObjects are private data fields. To access them you do so with a get method (getter or accessor). You'll just append get to the property name and finish it off with () to create a method: i.e. getNumberOfObject().
If the data field is a boolean type, you'll need to use "is" instead of "get."
To modify the private data field, you'll need a set method. Same concept as above except you replace get with set: i.e. setRadius().
In the following example we also use the keyword "static." All that means is that the value is accessible without instantiating the class, is not bound to the class and any objects that are created always point to the same memory location when it comes to that property. For example, if the numberOfObjects property wasn't declared as static, and myCircle1 and myCircle2 were instantiated, when you call numberOfObjects you'll get 1 for both myCircle1 and myCircle2 if you call them right after instantiating each object. On the other hand, if you set the numberOfObjects as static and you called the numberOfObjects right after instantiating each of the myCircle's, you'll get 1 after myCircle1 and 2 after myCircle2. That's because they point to the same memory location.
The TestCircleWithPrivateDataFields.java shows you how to use the setter and the accessor.
In CircleWithPrivateDataFields.java, the radius and numberOfObjects are private data fields. To access them you do so with a get method (getter or accessor). You'll just append get to the property name and finish it off with () to create a method: i.e. getNumberOfObject().
If the data field is a boolean type, you'll need to use "is" instead of "get."
To modify the private data field, you'll need a set method. Same concept as above except you replace get with set: i.e. setRadius().
In the following example we also use the keyword "static." All that means is that the value is accessible without instantiating the class, is not bound to the class and any objects that are created always point to the same memory location when it comes to that property. For example, if the numberOfObjects property wasn't declared as static, and myCircle1 and myCircle2 were instantiated, when you call numberOfObjects you'll get 1 for both myCircle1 and myCircle2 if you call them right after instantiating each object. On the other hand, if you set the numberOfObjects as static and you called the numberOfObjects right after instantiating each of the myCircle's, you'll get 1 after myCircle1 and 2 after myCircle2. That's because they point to the same memory location.
The TestCircleWithPrivateDataFields.java shows you how to use the setter and the accessor.
Comments
Post a Comment