Can create the getter and setter method private?
Can create the setter and getter method private ? If yes or 开发者_开发知识库no why?
Generally making setters as private methods is one of the many ways of making immutable objects.
Can create the setter and getter method private ? If yes or no why?
1) Yes you can do it.
2) The reason that you can do it is because the Java Language Specification says you can. As far as the JLS is concerned, there is nothing linguistically different between getter and setter methods and any other methods. And any method can be declared as private.
Your unstated question is why you would do it. And the reason is simple: to hide the methods from use outside of the class. And why might you do that? Here's a typical use-case:
We want to combine some logic (in this case enforcing of a constraint) with the setting (or getting) of an attribute, but we don't want the setter (or getter) to be visible:
public class Foo {
private Foo parent; // this must not change once it has been set
....
private void setParent(Foo parent) {
if (this.parent != null) {
throw new SomeException(...);
}
this.parent;
}
}
You can do it, but there is hardly a point. If those methods are private then any code that can call those methods can also access the objects properties directly.
Yes, getters and setters can be made private. If you want to create a JavaBean, then a private getter/setter fails the JavaBeans criteria, which states:
The class properties must be accessible using get, set, is (used for boolean properties instead of get) and other methods (so-called accessor methods and mutator methods), following a standard naming-convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties.
There is no reason why you can not make getters and setters private.
Why would you do this? If the setter method modified a property indirectly. For example, if your circle class had a radius property, then you could have setRadius, setDiameter, setCircumference, setArea methods and you could choose to make these methods private.
In fact, if you insert some convoluted code, you can have code that can call these setter/getter methods that can not modify the properties directly (without using reflection). But it is sufficiently convoluted that it seems hardly worth the trouble.
/**
* This convoluted class will not work correctly unless the line "this . xRef . set ( x )" is line 10 and the line "x = this . xRef . get ( ) ;" is line 23
**/
class Private
{
private void setX ( int x )
{
try
{
this . xRef . set ( x ) ;
}
catch ( IllegalAccessException cause )
{
assert false ;
}
}
private int getX ( )
{
int x ;
try
{
x = this . xRef . get ( ) ;
}
catch ( IllegalAccessException cause )
{
x = 0 ;
assert false ;
}
return x ;
}
private final Reference < Integer > xRef = new Reference < Integer > ( )
{
@ Override
public Integer get ( ) throws IllegalAccessException
{
checkMethod ( "Private" , "getX" , 23 ) ;
return super . get ( ) ;
}
@ Override
public void set ( Integer val ) throws IllegalAccessException
{
checkMethod ( "Private" , "setX" , 10 ) ;
super . set ( val ) ;
}
} ;
public static void main ( String [ ] args )
{
Private p = new Private ( ) ;
p . setX ( 5 ) ;
System . out . println ( p . getX ( ) ) ;
try
{
p . xRef . set ( 100 ) ;
}
catch ( IllegalAccessException cause )
{
cause . printStackTrace ( ) ;
}
System . out . println ( p . getX ( ) ) ;
}
}
class Reference < T >
{
private T val ;
public void set ( T val ) throws IllegalAccessException
{
this . val = val ;
}
public T get ( ) throws IllegalAccessException
{
return this . val ;
}
public void checkMethod ( String className , String methodName , int lineNumber ) throws IllegalAccessException
{
IllegalAccessException cause = new IllegalAccessException ( ) ;
boolean delta = true ;
for ( StackTraceElement e : cause . getStackTrace ( ) )
{
if ( ( e . getClassName ( ) . equals ( className ) ) && ( e . getMethodName ( ) . equals ( methodName ) ) && ( e . getLineNumber ( ) == lineNumber ) )
{
delta = false ;
break ;
}
}
if ( delta )
{
throw cause ;
}
}
}
精彩评论