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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CircleWithPrivateDataFields { | |
/** The Radius of the circle */ | |
private double radius = 1; | |
/** The number of objects created */ | |
private static int numberOfObjects = 0; | |
/** Construct a circle with radius 1 */ | |
public CircleWithPrivateDataFields() { | |
numberOfObjects++; | |
} | |
/** Construct a circle with a specified radius */ | |
public CircleWithPrivateDataFields(double newRadius) { | |
radius = newRadius; | |
numberOfObjects++; | |
} | |
/** Return radius */ | |
public double getRadius() { | |
return radius; | |
} | |
/** Set a new radius */ | |
public void setRadius(double newRadius) { | |
radius = (newRadius >= 0) ? newRadius : 0; | |
} | |
/** Return numberOfObjects */ | |
public static int getNumberOfObjects() { | |
return numberOfObjects; | |
} | |
/** Return the area of this circle */ | |
public double getArea() { | |
return radius * radius * Math.PI; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestCircleWithPrivateDataFields { | |
/** Main method */ | |
public static void main(String[] args) { | |
// Create a circle with radius 5.0 | |
CircleWithPrivateDataFields myCircle = new CircleWithPrivateDataFields(5.0); | |
System.out.println("The area of the circle of radius " + myCircle.getRadius() + " is " + myCircle.getArea()); | |
// Increase myCircle's radius by 10% | |
myCircle.setRadius(myCircle.getRadius() * 1.1); | |
System.out.println("The area of the circle of radius " + myCircle.getRadius() + " is " + myCircle.getArea()); | |
System.out.println("The number of objects created is " + CircleWithPrivateDataFields.getNumberOfObjects()); | |
} | |
} |
Comments
Post a Comment