7 Objects and Classes
Learn how Java classes model objects, organize state and behavior, create initialized instances, use references, and protect valid data through encapsulation.
Objects and Classes
An is a particular entity created while a Java program runs. It combines three related ideas:
State: the information currently stored, such as an account owner and balance.
Behavior: the actions the can perform, such as depositing or withdrawing money.
Identity: the fact that the is a distinct entity, even if another has identical data.
A is the definition or blueprint for objects. It describes the data objects of that contain and the operations they support. For example, a BankAccount can describe accounts, while each BankAccount stores its own account information.
A body commonly contains fields for state, constructors for initialization, and methods for behavior.
Takeaway: A defines what its objects are like; an is a particular run-time instance with its own identity and state.
State: Instance and Fields
A is a variable declared inside a . It stores information associated with an or with the as a whole.
An instance belongs separately to each . For example, first and second can refer to two BankAccount objects created with new BankAccount("Maya", 500.00) and new BankAccount("Luis", 800.00). The two objects have the same but different owner and balance values. Changing one 's balance does not normally change the other 's balance.
A is declared with static and is shared by all objects of the . For example, private static int accountCount = 0; represents one count associated with the rather than a separate count for every account.
Use instance fields for -specific state and static fields for information associated with the itself.
Takeaway: Decide whether a value belongs to one or is shared by the before choosing an instance or a .
Behavior Through Methods
A is a named block of code defined in a . It describes behavior, can change fields, return information, perform an action without returning a value, or accept parameters.
For example, a deposit can accept an amount and add it to the current 's balance only when the amount is valid. A withdraw can reject an invalid amount or an amount greater than the current balance, while getBalance can return the current balance.
Methods are called with the dot operator. For example, account.deposit(50.00) invokes deposit for the referred to by account, and account.getBalance() obtains information from that same . Calling the through a different reference operates on the other 's state.
Methods keep operations related to a concept together with the data those operations use.
Takeaway: Methods are the operations through which objects perform actions and expose useful information.
Creating and Initializing Objects
A is special code that runs when a new is created. It has the same name as its , has no return type, initializes the new 's fields, and may receive parameters.
For example, public BankAccount(String owner, double openingBalance) can initialize an account's owner and opening balance. The expression new BankAccount("Maya", 500.00) creates and initializes the by invoking that .
The statement BankAccount account = new BankAccount("Maya", 500.00); has three distinct parts:
BankAccount accountdeclares a whose type isBankAccount.new BankAccount("Maya", 500.00)creates and initializes an .The assignment stores a reference to that in
account.
Inside a or instance , the identifies the current . Thus, this.owner means the current 's , while owner by itself can mean a parameter with the same name.
If a declares no , Java can provide an implicit no-argument subject to the rules of the superclass. Once a declares a , Java does not automatically provide another no-argument . allows multiple constructors when their parameter lists differ. For example, Point() can delegate to Point(int x, int y) by using this(0, 0).
Takeaway: Constructors establish the conditions an should satisfy immediately after creation.
References, Types, and null
A has a type and stores a reference to an . The declaration BankAccount checking; declares the variable but does not create a BankAccount . The variable must later be assigned an , such as with checking = new BankAccount("Maya", 500.00);.
A -type variable can also contain null, which means it refers to no . For example, BankAccount account = null; does not provide an on which an instance can operate. Calling account.getBalance() in that state causes a NullPointerException at run time.
A name can be used as a variable type, parameter type, type, or return type. This allows a to pass one meaningful concept, such as a BankAccount, rather than passing unrelated pieces of data separately.
Declaring a and creating an are separate operations.
Takeaway: Always distinguish a reference from the it identifies, and ensure the reference is not null before using it.
and Design
protects an 's internal state by controlling how other code can access or change it. A private can be accessed directly only within its own .
For example, outside code should not directly assign an invalid value to a private balance . Instead, public methods such as account.deposit(100.00) and account.withdraw(25.00) can enforce the 's rules. A withdrawal can reject an amount that is not positive or exceeds the current balance.
This design keeps the responsible for preserving valid state and creates a small, understandable public interface. When designing a , ask:
What concept does the represent?
What state must each remember?
What actions should the perform?
What values are valid?
Which implementation details should remain hidden?
What must be true immediately after construction?
A well-designed keeps related data and operations together, prevents invalid state, and exposes only the operations that other code needs.
Takeaway: hides implementation details and lets a control the valid ways its state can change.
Putting the Parts Together
Consider a Student with private name and credits fields. Its establishes the initial state, addCredits changes the state only for valid input, and getCredits, getName, and isFullTime provide controlled access and useful behavior.
A typical sequence is Student student = new Student("Avery");, followed by student.addCredits(15);. The variable student refers to an instance of Student. The begins with the state established by its , and addCredits updates that 's credits. Query methods read the resulting state without exposing the fields for arbitrary direct modification.
In this example, Student is a and a reference type, student is a , and the created with new Student("Avery") is an instance of Student. The fields represent state, the methods provide behavior, and controls how the state changes or is examined.
This pattern generalizes to other classes: define a concept, store its individual state in instance fields, initialize that state in constructors, provide behavior through methods, and protect the 's rules through controlled access.
Final takeaway: Read -oriented Java by tracking four questions: what defines the , what state the stores, what created it, and which methods can validly change or examine that state.