Title: static methods
1static methods
- COSC 101
- Code available at
- http//cs.colgate.edu/faculty/jspacco/101/examples
/bankAccountRevisited.zip
2There are three kinds of methods
- instance methods
- static methods
- constructors
- Constructors are special instance methods
3Using constructors
- Its like a method call
- but no instance yet
- because were creating a new instance
- no dot
- must use the keyword new
- BankAccount b1new BankAccount()
4Defining constructors
- If you dont define any constructors
- Java will create a default or void constructor
for you - Takes no parameters
- initializes all fields to default value for that
type - 0 for primitive types
- null for reference types
- If you create at least one constructor that takes
at least one parameter - Java will not create the default constructor for
you - so if you want a default constructor
- provide one yourself
5Defining constructors, cont
- Method declaration for constructor
- no return type
- method name must match the name of the class
exactly - Can take parameters
6Classes can have as many constructors as you want
- Constructors are differentiated from one another
by their parameters - This process is called method overloading
7Defining multiple constructors
- public BankAccount(double balance)
- this.balancebalance
-
- public BankAccount(String accountNum)
- this.accountNumberaccountNum
-
- public BankAccount(double balance,
- String accountNum)
- this.balancebalance
- this.accountNumberaccountNum
8Java differentiates between constructors by their
parameters
- BankAccount b1new BankAccount()
- BankAccount b2new BankAccount(1000.0,
- 123abc)
- BankAccount b3new BankAccount(2000.0)
9static VS instance
- instance methods
- can manipulate instance variables
- must be invoked on an instance
- b1.withdraw(100.0)
- static methods
- cannot see instance variables
- have no state
- COSC101Math.power(1.05, 8)
10Instance methods
- Inputs Parameters passed into method
- methods can have zero or more parameters
- Output return value
- methods can only return one value
- Side-effects
- instance methods can manipulate the instance
variables of the class - which might have the side-effect of changing the
state of the method
11static methods
- state-less methods
- cannot manipulate or even see instance variables
- if a method doesnt look at any instance
variables at all - it should probably be static
- Common to put lots of static utility methods into
one class - java.lang.Math
- http//java.sun.com/j2se/1.5.0/docs/api/java/lang/
Math.html
12All primitives in Java are passed by value
- This just means that we actually pass a copy of
each parameter of primitive type - primitives are small
- 1 to 8 bytes each
- very efficient just to copy them
- BankAccount b1new BankAccount(1000.0)
- double money100.0
- b1.deposit(money)
13All reference types are passed by reference
- This means that just a reference (or handle) to
the object gets passed into the method - The object still only exists in one place in
memory - 500.0 is passed by value
- b2 is passed by reference
- BankAccount b1new BankAccount(1000.0)
- BankAccount b2new BankAccount(2000.0)
- b1.transfer(500.0, b2)
14Parameters passed by value cannot change the
original value
- You dont really have the original value
- you just have a copy of it!
- Thus even if you change the value of a primitive
passed into a method - it DOES NOT change the value of the original
thing passed in
15Parameters passed by reference can be changed
inside the method
- And these changes are visible outside the method
- This is because you have a reference to the
actual object - For example, the transfer method may change the
balance of b2 - BankAccount b1new BankAccount(1000.0)
- BankAccount b2new BankAccount(2000.0)
- b1.transfer(500.0, b2)
16private methods
- private methods are also called helper methods
- These methods are only usable inside the class
- In the BankAccount class, we have to remove money
from the balance - and return true if it was OK to take that money
out - false if there wasnt enough money
17Lets add a deductMoney method
- private instance method
- because its private, it can only be used inside
the BankAccount class - private boolean deductMoney(double m)
-
- if (balance gt m)
- balance-m
- return true
-
- return false
18deductMoney method can simplify other methods
- transfer method can be simplified using
deductMoney money methods - that means that one method will call another
method
19Before
- public boolean transfer(double m, BankAccount
other) -
- if (this.balance gt m)
- other.balancem
- this.balance-m
- return true
-
- return false
20After
- public boolean transfer(double m, BankAccount
other) -
- if (deduct(m))
- other.deposit(m)
- return true
-
- return false
-
21Calling instance methods from other instance
methods
- You can use the this keyword
- this.deductMoney(m)
- Or not
- deductMoney(m)
- this is implicitly the reference to the instance
upon which a method is being invoked - can be used to access fields
- or to invoke methods
22Lets play definition or use
- Know the difference between defining a method and
using a method