title: Check and retain the previous object instead of creating a new one
body: <p>I am new to Java.</p>
  <p>I created a banking system where user can pay their utility bills. I want to keep that status of the bills whether it is paid or not in <code>Bills</code> class. But whenever user wants to go to pay their bills and it creates a new object which vipes the previous data stored in that object. I want something to check if an object already exists it uses that but if it does not exists then create an object and use that. Please do let me know if it is possible.</p>
  <p>Look at <code>case 2</code> where it makes a new object everytime.</p>
  <pre><code>import java.util.Scanner;
  import java.util.Random;
  
  public class Main {
      static boolean isAccount = false;
  
      public static void main(String[] args) {
          Menu menu = new Menu(isAccount);
  
          menu.showMenu();
          menu.take();
      }
  }
  
  class Menu {
  
      int option = 0;
      boolean isAccount;
      double money;
      Scanner input = new Scanner(System.in);
  
      public Menu(boolean isAccount) {
          this.isAccount = isAccount;
      }
  
      public void showMenu() {
          System.out.println(&quot;Choose your option: \n1. Create an Account\n2. Pay Bills\n3. Banking&quot;);
      }
  
      public void take() {
          if (input.hasNextInt()) {
              int opt = input.nextInt();
              this.option = opt;
          }
          
          System.out.println(&quot;Option value: &quot; + this.option);
  
          navigate();
      }
  
      public void navigate() {
          while (this.option != -1) {
  
              switch (this.option) {
                  case 1: 
  
                      if (!isAccount) {
                          Scanner nameInput = new Scanner(System.in);
                          System.out.print(&quot;Enter your name: &quot;);
                          String name;
                          name = nameInput.next();
                          
                          Random rand = new Random();
                          int id, number;
                          do {
                                  number = rand.nextInt(99999); 
                                  id = number;
                              } while (number &lt; 10000);
      
                          Scanner accountInput = new Scanner(System.in);
                          System.out.print(&quot;Do you want current account (true/false): &quot;);
                          boolean isCurrent = accountInput.nextBoolean();
                          
                          Account acc = new Account(name, id, isCurrent);
                          this.money = acc.getMoney();
                          acc.print();
                          
                          isAccount = true;
                      } else {
                          System.out.println(&quot;You already have an account!&quot;);
                      }
                  
                      break;
  
                  case 2:
  
                      if (isAccount) {
  
                          System.out.println(&quot;Money: &quot; + this.money);
  
                          Bill bill = new Bill(this.money);
  
                          bill.showBills();
                          bill.selectBill();
  
                          this.money = bill.getMoney();
  
                      } else {
                          System.out.println(&quot;Please make an account before you pay the bill!&quot;);
                          showMenu();
                          take();
                      }
  
                      break;
  
                  case 3:
                      if (isAccount) {
                          Banking bank = new Banking(money);
                          bank.showMenu();
                          bank.takeOption();
                          this.money = bank.getMoney();
                          System.out.println(&quot;\nNew Bank Balance: &quot; + this.money + &quot;\n&quot;);
                      } else {
                          System.out.println(&quot;Please make an account before you do banking&quot;);
                          showMenu();
                          take();
                      }
              }
  
              this.showMenu();
              this.take();
          }
      }
  }
  </code></pre>
  <p>here's the bills class</p>
  <pre><code>import java.util.Scanner;
  import java.util.Random;
  
  class Bill {
  
      class Paid {
          boolean electricity, water, gas, internet = false;
      }
  
      int option;
      double money;
      Paid paidBills = new Paid();
  
      public Bill(double money) {
          this.money = money;
      }
      
      public void showBills () {
          System.out.println(&quot;\nWhich bill do you want to pay?&quot;);
          System.out.println(&quot;1. Electricity Bill\n2. Gas Bill\n3. Water Bill\n4. Internet Bill\n5. See Bills Status&quot;);
  
      }
  
      public void selectBill() {
  
          Scanner input = new Scanner(System.in);
          option = input.nextInt();
          navigate(this.option);
      }
  
      public void navigate(int option) {
  
              switch (option) {
                  case 1:
                      Electricity ebill = new Electricity(this.money);
                      double tempEBill = Math.floor(ebill.makeBill());
                      ebill.showBill(tempEBill);
                      this.money = ebill.pay(tempEBill);
                      ebill.receipt(tempEBill, this.money);
                      paidBills.electricity = true;
                      break;
                  case 2:
                      Gas gbill = new Gas(money);
                      double tempGBill = Math.floor(gbill.makeBill());
                      gbill.showBill(tempGBill);
                      this.money = gbill.pay(tempGBill);
                      gbill.receipt(tempGBill, this.money);
                      paidBills.gas = true;
                      break;
                  case 3:
                      Water wbill = new Water(money);
                      double tempWBill = Math.floor(wbill.makeBill());
                      wbill.showBill(tempWBill);
                      this.money = wbill.pay(tempWBill);
                      wbill.receipt(tempWBill, this.money);
                      paidBills.water = true;
                      break;
                  case 4:
                      Internet ibill = new Internet(money);
                      double tempIBill = Math.floor(ibill.makeBill());
                      ibill.showBill(tempIBill);
                      this.money = ibill.pay(tempIBill);
                      ibill.receipt(tempIBill, this.money);
                      paidBills.internet = true;
                      break;
                  case 5:
                      showPaidBills();
                      break;
              }
  
      }
  
      public void showPaidBills() {
          System.out.println(&quot;\nElectricty: &quot; + paidBills.electricity + &quot;\nWater: &quot; + paidBills.water + &quot;\nGas: &quot; + paidBills.gas + &quot;\nInternet: &quot; + paidBills.internet + &quot;\n&quot;);
      }
  
      public double getMoney() {
          return this.money;
      }
  }
  
  class Electricity {
  
      double money;
      static double bill;
  
      public Electricity(double money) {
          this.money = money;
      }
  
      public double makeBill() {
          Random rand = new Random();
          bill = rand.nextDouble() * 1000;
          return bill;
      }
  
      public void showBill(double tempBill) {
          System.out.println(&quot;\nElectricity Bill: &quot; + tempBill);
      }
  
      public double pay(double tempBill) {
          return money - tempBill; 
      }
  
      public void receipt(double tempBill, double money) {
          System.out.println(&quot;Bill paid amount = &quot; + tempBill);
          System.out.println(&quot;Remaining Amount in Bank = &quot; + money + &quot;\n&quot;);
      }
  }
  
  class Water extends Electricity {
      public Water(double money) {
          super(money);
      }
  
      public void showBill(double tempBill) {
          System.out.println(&quot;\nWater Bill: &quot; + tempBill);
      }
  }
  class Gas extends Electricity {
      public Gas(double money) {
          super(money);
      }
  
      public void showBill(double tempBill) {
          System.out.println(&quot;\nGas Bill: &quot; + tempBill);
      }
  }
  class Internet extends Electricity {
      public Internet(double money) {
          super(money);
      }
  
      public void showBill(double tempBill) {
          System.out.println(&quot;\nInternet Bill: &quot; + tempBill);
      }
  }
  </code></pre>