private method can be used only by the other methods of the object. Program code outside the object cannot directly use a private method of the object.
Say that the bank wants to keep track of how many times each checking account is used.
A "use" of the checking account is processing a deposit processDeposit() or processing a check processCheck() .
Private Methods Example: incrementUse( )
To do this, we add the following members to CheckingAccount class:
A private variable "useCount“
A private method “incrementUse”
The incrementUse() is used by processDeposit() and processCheck() methods each time they are used, to increase the value of useCount by (1).
CheckingAccount class with incrementUse( )
class CheckingAccount {
private String accountNumber;
private String accountHolder;
private int balance;
private int useCount = 0;
private void incrementUse( ) {
useCount = useCount + 1;
}
void processDeposit( int amount ) {
balance = balance + amount ;
incrementUse( );
}
void processCheck( int amount ) {
balance = balance – amount;
incrementUse( );
}
}
main can’t use private method
The main() cannot use the private variable useCount nor can it use the private method incrementUse().
The main() can use bobsAccount.processCheck() which is not private. It in turn uses the private method incrementUse().
class CheckingAccountTester {
public static void main( String[ ] args ) {
CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );
bobsAccount.processCheck( 50 );
}
}
display( ) Method
As main() cannot access the private variables, so it is good idea to have a display() method to print the private variables.
The public access modifier explicitly says that a method or variable of an object can be accessed by code outside of the object.
The access methods are intended for outsiders, and have to be visible to outsiders in order to be useful.
So, the public visibility modifier is usually used for all access methods and constructors in a class definition.
The public Visibility Modifier
class CheckingAccount {
private String accountNumber;
private String accountHolder;
private int balance;
private int useCount = 0;
private void incrementUse( ) { .........}
public CheckingAccount( String accNumber, String holder, int start ) { . . . . }
public void processDeposit( int amount ) { . . . . }
public void processCheck( int amount ) { . . . . }
public int currentBalance( ) { . . . . }
public void display( ) { . . . . }
}
Default Visibility
If you do not specify public or private for a variable or a method, then it will have default visibility.
Default visibility allows a variable or method to be seen within all methods of a class or other classes that are part of the same package .
For now, default visibility means about the same thing as public visibility. But it is best to explicitly declare members public if that is what you actually need.
You can not explicitly declare a member of a class to have default visibility (for example, you can't say default int monthlyCharge; )
Parameters, Local Variables, Overloading
Parameters and Local Variables
An object stores its state as values in its instance variables.
But sometimes an object's methods work with values that are not part of an object's state.
These values are stored in the parameters and local variables .
A parameter is a variable name used in a method definition.
class CheckingAccount {
. . . .
private int balance;
. . . .
void processDeposit(int amount ) {
balance = balance + amount ;
}
}
What is a Parameter
What is the use of a Parameter
It is used to store the values passed to the method by its callers.
In this example, the value 200 passed by main() is stored in the parameter “ amount”
class CheckingAccountTester { public static void main(String[] args) { CheckingAccount bobsAccount= new CheckingAccount("999", "Bob", 100 ); bobsAccount.processDeposit( 200 ); } }
Formal and Actual Parameters
Formal parameter —the identifier used in a method definition in which the value is passed.
amount is a formal parameter of processDeposit
Actual parameter —the actual value that is passed into the method by a caller.
200 is an actual parameter of processDeposit .
Scope of a Parameter
The scope of a parameter is the body of its method.
The display() method is outside the scope of amount .
Local Variables
A local variable is a variable that is declared inside of the body of a method.
It is used to hold a temporary value.
class CheckingAccount { private int balance; void processCheck(int amount) { int charge; // scope of charge starts here ........................ // scope of charge ends here } }
Method Overloading
Overloading is when two or more methods of a class have the same name but have different parameter lists .
When one of the methods is called, it is made clear which one is wanted by matching the actual parameter list to the formal parameter lists .
Method Overloading class CheckingAccount { private int balance; . . . . void processDeposit( int amount ) { balance = balance + amount ; } void processDeposit( int amount, int serviceCharge ) { balance = balance + amount - serviceCharge; } } class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 ); bobsAccount.processDeposit( 200 ); // call to first bobsAccount.processDeposit( 200, 25 ); // call to second } }
Method Signature
The signature of a method is:
Its name.
The number and types of its parameters, in order.
For example, the signatures of the two processDeposit methods are:
processDeposit( int )
processDeposit( int, int )
The signatures of the methods in a class must be unique.
The return type is not part of the signature
Call by Value & Primitive Parameters
Java uses call by value for parameters.
Changes to the primitive parameter do not affect the Caller
class SimpleClass { public void work( int x ) { x = 100; // local change to the formal parameter } } class SimpleTester { public static void main ( String[] args ) { int var = 7; SimpleClass simple = new SimpleClass(); System.out.println("First value of the local var: " + var ); simple.work( var ); System.out.println("Second value of the local var: " + var ); } } Output First value of the local var: 7 Second value of the local var: 7
Object Parameters
An object can also be passed as a parameter to a method.
Again call by value is used, but now the value is a reference to an object.
Using a object reference parameter, the contents of a mutable object can be changed, and the change will affect the caller.
However, Immutable objects (like String objects) can‘t be changed
class MyPoint {
public int x=3, y=5 ;
public void print() {
System.out.println("x = " + x + "; y = " + y );
} }
class PointDoubler {
public void twice( MyPoint parm ) {
parm.x = parm.x * 2 ;
parm.y = parm.y * 2 ;
} }
class PointTester {
public static void main ( String[ ] args ) {
MyPoint pt = new MyPoint( );
PointDoubler dbl = new PointDoubler( );
pt.print( );
dbl.twice( pt );
pt.print();
} }
Mutable Object Parameters: MyPoint x = 3; y = 5 x = 6; y = 10
class ObjectPrinter {
public void print( String st ) {
st = "Hah! A second Object!" ;
}}
class OPTester2 {
public static void main ( String[] args ) {
String message = “Welcome" ;
ObjectPrinter op = new ObjectPrinter( );
System.out.println("First value of message: " + message );
op.print( message );
System.out.println("Second value of message: " + message );
} }
Immutable Object Parameters: String First value of message: Welcome Second value of message: Welcome
0 comments
Post a comment