private and public. static
. this protected and restudy the
meaning of private and public final.super.
public class BankAccount {
<---- class body here
}
The class contains BankAccount(String name, int saving) )private int saving)public String getName())BankAccount class's constructor is
BankAccount , the name of the CheckingAccount
class's constructor is CheckingAccount.BankAccount defines a single constructor:
public BankAccount(String name, int saving) {
this.name = name;
this.saving = saving;
}
public BankAccount(String name) {
this(name,0);
}
BankAccount ,
but they have different parameter lists.String (name)
and and int (saving)
String (name)
new BankAccount("Charles");
BankAccount uses the following lines of
code to define its member variables:
private String name;
private int saving;
name, saving
, and their data type are String ,int respectively. Also,
the private keyword identifies them as a private
members. This means that only codes within the BankAccount
class can access them.private, protected, public
or nothing), type, name.staticfinal
SPEED_OF_LIGHT , whose value is the speed of light in vacuum =
299,792,458 m/s (meters per second).
It's a compile-time error if your program ever tries to change a final variable. By convention, the name of constant values are spelled in uppercase letters.final long SPEED_OF_LIGHT = 299792458;
typeint ,
float , or boolean . Or you can use reference types,
such as array, object, or interface names. access return method arguments
level type name |
| | | |
| | | |
V V V V
public void deposit(int depositAmount) { <----- method declaration
if(depositAmount >= 0) {
saving+= depositAmount; <----- method body
}
}
static static declares this
method as a class method rather than an instance method. finalvoid for the return type. ( paramlist )void as the return type if there is
no return.
public int getSaving() {
return saving;
}
public class OverloadTest {
public void method() {
System.out.println("no argument");
}
public int method(int a) {
System.out.println("argument list : int");
return 1;
}
public int method(String s) {
System.out.println("argument list : String");
return 1;
}
public char method(int a, String b) {
System.out.println("argument list : int, string");
return 'a';
}
public static void main(String[] args) {
OverloadTest test = new OverloadTest();
test.method();
test.method(1);
test.method("hi");
test.method(1,"Java");
}
}
double computePayment(double loanAmt, double rate,
double futureValue,
int numPeriods) {
double I, partial1, denominator, answer;
I = rate / 100.0;
partial1 = Math.pow((1 + I), (0.0 - numPeriods));
denominator = (1 - partial1) / I;
answer = ((-1 * loanAmt) / denominator)
- ((futureValue * partial1) / denominator);
return answer;
}
computePayment method, and
reference data types such as objects and arrays.public double method(BankAccount account, int month);
public int sum(int[] intArray);
this is a reference to the object itself. public BankAccount(String name, int saving) {
this.name = name;
this.saving = saving;
}
name = name is ambigous.this keyword to call one of the
current object's variables and methods. This is only necessary if
there is some ambiguity in the method name and is often used to make
the intent of the code clearer.this to call other constructors. e.g.
public BankAccount(String name) {
this(name,0);
}
The line this(name,0) call the constructor BankAccount(name,0)
and instantiate an object with name and 0 savingstatic
and final. final type variable means the variable cannot
be changed this