breadfan18
breadfan18

Reputation: 49

Passing boolean argument in method but method boolean variable is still unused

A bit of a Java noob here. So I am calling a method called ageRestrProcessor() that takes a boolean ageValidationStatus argument, into the Processing class. The Processing class has a boolean variable ageNotValidated already initialized to true. So now, I'm trying to pass the ageNotValidated variable into the ageRestrProcessor() method as an argument, and then within the method, trying to change the boolean value to false. But, in the method itself, it's saying the boolean variable ageValidationStatus is unused. So obviously that boolean value never gets changed. On the surface, it seems this should work but i can't figure out what the problem is. Thanks in advance for your responses.

//ItemProcessors class

public class ItemsProcessors {
  void ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
    double ageRstrItemPrice = bp.roundUpToTwoDecimals(itemUnitPriceFromDB);
    String dollarAgeRestPrice = bp.addDollarFormat(ageRstrItemPrice);

    if (ageValidator){
        System.out.println("\tAge Validation Successful");
        System.out.println("\tTotal Price: " + dollarAgeRestPrice);
        map.put(keyNum, new Object[] {itemNameFromDB, itemUnitPriceFromDB, itemUnit, ageRstrItemPrice, dollarAgeRestPrice});

        //Here, the compiler is telling me that ageNotValidated variable is never used.
        ageValidationStatus = false;
    }
    else {
        System.out.println("\tShopper is underage. Item not added");
    }
  }
}

//I'm calling the ageRestrProcessor method into this Processing class

public class Processing extends OrderData {
   private Map<Integer, Object[]> itemMap = new HashMap<Integer, Object[]>();
   BasePage bp = new BasePage();
   private Exceptions xcep = new Exceptions();
   private ItemsProcessors ip = new ItemsProcessors();
   public boolean ageNotValidated = true;

   public void itemsProcessor(XSSFSheet itemSheet){
      if (xcep.isAgeRestricted(itemSheet, rowNum) && ageNotValidated){
         String ageEntered = bp.getStringFromScanner("\tEnter DOB in 'mm-dd-yyyy' format: ");
         boolean isAgeValid = xcep.ageValidator(ageEntered);

         //I'm trying to pass the ageNotValidated variable into the method here.
         ip.ageRestrProcessor(keynum++, itemNameFromDB, itemUnitPriceFromDB, ageNotValidated, isAgeValid, itemUnit, itemMap);
      }
    }
}

Upvotes: 2

Views: 26068

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

ageValidationStatus is created inside ageRestrProcessor method and cannot be used outside this method( local variable) and it's not being used anywhere else in the current method as well

so it's a warning by compiler to improve code quality , unnecessary assignment of value when it has no use elsewhere


Make ageRestrProcessor return boolean flag and use value accordingly

  boolean ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
         //..code
         // you can remove  boolean ageValidationStatus from method signature
    if (ageValidator){
         //..code
         return false;
    }
    else {
        System.out.println("\tShopper is underage. Item not added");
        return true;
    }
  }

and

ageNotValidated = ip.ageRestrProcessor(keynum++,....;

Upvotes: 2

Geoduck
Geoduck

Reputation: 8995

I think you may be confusing passing values value and passing by reference. By default, primitive types in Java are passed by value.

That means, a copy is made. Changing the value of a by-value parameter in a method only changes the copy, it does not change the variable that was used to pass it in.

When passing by value, the parameter does not need to be a variable. You can pass in literal values:

myfunction(false);

Also, setting a value of a variable does not "use" the variable. You can set it, but if it is never read then it is considered "dead code" because it has no effect. Because of that, you get a warning.

In your scenario, I would suggest having ageRestrProcessor() return a boolean value to indicate if age was verified. The caller can change the original boolean accordingly.

Upvotes: 2

Related Questions