Java Ex4
Java Ex4
Aim
To demonstrate static members and methods.
Algorithm
Step 1: Define a class named BankAccount with accountHolder and balance as
its non-static members, and totalAccounts as a static member.
Step 2: Define a constructor to initialize the account holder and their balance,
and to increment totalAccounts.
Step 3: Define a non-static method named displayAccountDetails() to display
the account details.
Step 4: Define a static method named displayTotalAccounts() to display the
number of accounts created.
Step 5: Create two different instances of the BankAccount class and invoke
both the static and non-static methods.
Source Code
class BankAccount
{
static int totalAccounts = 0; // Static variable to count total accounts
String accountHolder;
double balance;
void displayAccountDetails()
{
System.out.println("Account Holder: " + accountHolder);
System.out.println("Balance: " + balance);
}
MyClass.java
class MyClass
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount("Anand", 1500.00);
BankAccount account2 = new BankAccount("Arun", 2500.00);
account1.displayAccountDetails();
account2.displayAccountDetails();