Creating an object inside an if statement [duplicate]

两盒软妹~` 提交于 2021-02-17 05:42:13

问题


I'm trying to make a simple program for a bank account. I have created a class called Bank to make and instance and in the main class, where the main method is, I have made an if statement which will create an instance of the class "Bank" depending on the conditions met. The problem is that I can use the instance methods, which is outside the if statement. I have created two constructors for the object class, one with a constructor method parameter and another method which doesnt take any parameters, which is the reason of using an if statement.

 public static void start() {
      Scanner scanner = new Scanner(System.in);

      System.out.println("Welcome to your banking app!");
      System.out.println("What is your initial balance, enter below. If none enter : n");
      String choice = scanner.nextLine();

      if(choice.equals("n")){
           Bank account1 = new Bank();
      }
      else{
           System.out.println("Enter your initial balance :");
           double ibalance = scanner.nextDouble();
           Bank account1 = new Bank(ibalance);
      }

      System.out.println("Enter 1 to see balance Enter 2 to withdraw  Enter 3 to deposit money Enter 4 to close account Enter 5 to exit");
      choice = scanner.nextLine();
      double amount = 0.0; 
      if(choice.equals("1")){
           System.out.println("Balance is :" + account1.getBalance());
      }

      else if(choice.equals("2")){
           System.out.println("Enter the amount to withdraw");
           amount = scanner.nextInt();
           account1.withdraw(amount);
      }

      else if(choice.equals("3")){
           System.out.println("Enter the amount to deposit");
           amount = scanner.nextInt();
           account1.deposit(amount);
      }

      else if(choice.equals("4")){
           account1.close();
      }

      else if(choice.equals("5")){
           System.exit(0);
      }
 }

回答1:


your Bank object will only live in if clause. should change to :

Bank account1 = null;
if (clause){
    account1 = new Bank();
}else{
  ...
}



回答2:


declare the Bank object outside of the if else scope, so you can access to it later, initialize that to null and assign a reference to a new instance of Bank depending on the condition if-else

Bank account1 = null;
if(choice.equals("n")){
    account1 = new Bank();
} else{
    System.out.println("Enter your initial balance :");
    double ibalance = scanner.nextDouble();
    account1 = new Bank(ibalance);
}



回答3:


Create an instance of the Bank class that is not initialised. Then initialise it where needed. Note that the account1 variable is created outside of your nested conditional statements.

   Bank account1;

   if(choice.equals("n"))
      account1 = new Bank();
   else
   {
       System.out.println("Enter your initial balance :");
       double ibalance = scanner.nextDouble();
       account1 = new Bank(ibalance);
   }

You want to reuse the account1 variable as much as you can in this instance instead of creating new instances.



来源:https://stackoverflow.com/questions/44022664/creating-an-object-inside-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!