What is state of an object in Java with example

Mary 38 Published: 11/03/2024

What is state of an object in Java with example

I'd be happy to explain the concept of state in Java and provide examples.

What is State?

In programming, the state of an object refers to its current situation or condition. It's the combination of data members (attributes or properties) that define the object's status at a particular moment. In other words, it's the object's "state of being." An object can be in various states depending on its attributes and relationships with other objects.

Java Example:

Let's consider a simple example of a BankAccount class:

public class BankAccount {

private double balance;

private String accountHolder;

public BankAccount(double initialBalance, String accountHolder) {

this.balance = initialBalance;

this.accountHolder = accountHolder;

}

public void deposit(double amount) {

balance += amount;

}

public void withdraw(double amount) {

if (amount <= balance) {

balance -= amount;

} else {

System.out.println("Insufficient funds!");

}

}

public double getBalance() {

return balance;

}

public String getAccountHolder() {

return accountHolder;

}

}

In this example, the BankAccount object has two attributes: balance and accountHolder. The state of a BankAccount object can be described as:

Initial State: When a new BankAccount is created, its balance is set to the initial value provided in the constructor, and the account holder's name is also set. Deposit State: After calling the deposit() method, the balance increases by the deposited amount. The object remains in this state until another operation changes it. Withdrawal State: When withdrawing an amount that does not exceed the current balance, the balance decreases accordingly. If the withdrawal exceeds the available funds, the object enters a "State of Insufficiency" (represented by a console message). Account Holder Change State: If the accountHolder attribute is updated using the setAccountHolder() method, the object's state changes to reflect the new account holder.

Key Takeaways:

The state of an object is determined by its attributes and relationships with other objects. Objects can transition between states based on their methods or interactions. Understanding an object's state is crucial for designing robust and predictable programs in Java (and other programming languages).

I hope this explanation helps you grasp the concept of state in Java!

Java state class

Here's a detailed explanation of the Java State class:

In object-oriented programming (OOP), a state is an object that represents the current condition or status of another object, such as its properties or attributes. In Java, we can represent this concept using the State interface.

The State interface is part of the Flyweight design pattern, which allows you to create objects with many instances and share them efficiently. The idea is to use a single instance for multiple states that have the same characteristics (attributes or properties).

Here's an example of how we can implement the State interface in Java:

public interface State {

void doSomething();

}

public class ConcreteState implements State {

private String stateName;

public ConcreteState(String stateName) {

this.stateName = stateName;

}

@Override

public void doSomething() {

System.out.println("Current state is " + stateName);

}

}

In the above example, we have a ConcreteState class that implements the State interface. The doSomething() method in this class simply prints out the current state name.

To create instances of the ConcreteState, we can use the following code:

public class Main {

public static void main(String[] args) {

State state1 = new ConcreteState("state1");

State state2 = new ConcreteState("state2");

state1.doSomething();

state2.doSomething();

}

}

In this example, we create two instances of the ConcreteState and then call the doSomething() method on both. The output will be:

Current state is state1

Current state is state2

This demonstrates how we can use the same instance (state) for different objects with the same properties (attributes).

By using the State interface, we can decouple the behavior of an object from its underlying state. This allows us to change the state without modifying the object itself.

In real-world scenarios, this concept is useful when you have objects that can be in multiple states (e.g., a user who can be logged in or not). By using the State pattern, you can represent these different states and behaviors independently of the objects themselves.