FileDocCategorySizeDatePackage
FriendAccess.javaAPI DocExample2380Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.review

FriendAccess

public class FriendAccess extends Object
Demonstrates How friend instances have access to each other's internals jsn gig ueg ioe g.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.3 $

Fields Summary
private int
value
Holds the value of the property value.
Constructors Summary
public FriendAccess(int value)
Creates a new instance of FriendAccess

param
value Initial value for the instance

		setValue(value);
	
Methods Summary
public intgetValue()
Getter for the property value.

return
The current value.

		return value;
	
public static voidmain(java.lang.String[] args)
Main demonstration method.

param
args the command line arguments

		final FriendAccess one = new FriendAccess(5);
		final FriendAccess two = new FriendAccess(5);

		try {
			two.setValue(25);
		} catch (final IllegalArgumentException ex) {
			System.out.println("Settign value to 25 through setter rejected");
		}

		one.someMethod(two);
		System.out.println("Settign value to 25 through friend ALLOWED!!!");
		System.out.println(two.getValue());
	
public voidsetValue(int value)
Setter for the property value. Doesnt allow values greater than 10.

param
value The new value.
throws
IllegalArgumentException If the value is greater than 10.

		if (value > 10) {
			throw new IllegalArgumentException();
		}
		this.value = value;
	
public voidsomeMethod(oreilly.hcj.review.FriendAccess obj)
If the comparison instance and this instance are both 5 then set the compare object's value to 25.

param
obj object to compare to.

		if ((obj.value == 5) && (this.value == 5)) {
			obj.value = 25;  // <= Ouch, this works and bypasses the setter.
		}