readNumber
, getSeparateDigits
, testValidity
, and writeNumber
.Book
object: Manipulated by getTitle
, getAuthor
, isOnShelf
, isFiction
, and goOutOfPrint
.ListOfBooks
object: Manipulated by search
, addBook
, removeBook
, and sortByAuthor
.public
: The class is usable by all client programs (code outside the class).private
: Restricts access.static
: A static variable (class variable) is shared by all instances of the class; memory allocation happens once.Math
class constants).ClassName.VARIABLE_NAME
; within the class, it is referred to as VARIABLE_NAME
.accessSpecifier returnType methodName(parameterList)
void
indicates no return value.{}
block, follows the header.Constructors create objects of a class, sharing the same name as the class and having no return type.
Multiple constructors allow for different object initialization methods.
No-argument constructor: Provides default initial values.
BankAccount b = new BankAccount();
initializes a BankAccount
object with a zero balance and an empty string password.new
operator returns the address of the new object, which is assigned to the object variable (reference).Constructor with parameters:
BankAccount c = new BankAccount("KevinC", 800.00);
b
and c
are object variables that hold the memory addresses (references) of their BankAccount
objects..
operator indicates that getBalance()
is a method of the BankAccount
class.Static methods perform operations for the entire class, not individual objects.
They are declared using the static
keyword in their header.
Static methods cannot directly call instance methods or access private instance variables because there's no implied object.
They can use static variables.
Example: A static method to get the employee count in an Employee
class.
public static int getEmployeeCount() { return employeeCount; }
Example: A static method to retrieve the interest rate for all bank accounts.
Instance methods are called using an object variable (e.g., object.method()
).
Static methods are called using the class name (e.g., ClassName.method()
).
Driver Class:
Static methods within a driver class containing main()
must be static.
The main()
method needs to be static since, at the start of program execution, no objects exist yet.
{}
).this
Keywordthis
refers to the implicit parameter (the object for which the method is called) within an instance method.this
prefix (e.g., this.variableName
).obj.doSomething("Mary", num)
, "Mary"
and num
are explicit parameters, while obj
is the implicit parameter.this
:printPerson
method: System.out.println(this);
(invokes the toString
method).Reference vs. Primitive Data Types:
double
, int
, char
, boolean
.String
, Random
, int[]
, String[][]
, custom classes).Declaration of objects:
Date d = new Date(2, 17, 1948);
* This creates a reference variable d that refers to a Date object. The value of d is the address in memory of that object
Aliasing: Having two references to the same object.
Example:
Date birthday = d;
Changes made through one reference affect the other.
Null Reference
You can test whether a variable refers to an object or is uninitialized by using the keyword null
Example:
if (b == null)
The declaration BankAccount b; defines a reference b that is uninitialized. This is known as uninitialized object variable or a null reference/pointer.
If a reference is not null, it can be set to null with the statement b = null;
Compile-time vs Run-time errors:
Formal vs. Actual Parameters:
acctPassword
and amount
in withdraw(String acctPassword, double amount)
)."TimB"
and 250
in b.withdraw("TimB", 250)
).Passing Primitive Types as Parameters
Passing Objects as Parameters
In Java both primitive types and object references are passed by value
The reference is unchanged throughout, however any value associated is changed.
Returning an object from a method means that you are returning the address of the object (does not create a new object).