-2

Given the code below, why would the compiler complain at line 5 even though coupon, offset, and base have been initialized within the if statement? Since 'percent' variable is given a default value of 0.0, the if statement will run and set those values.

public class Discounter {
static double percent; //1
int offset = 10, base= 50; //2
public static double calc(double value) {
    var coupon, offset, base; //3
    if(percent <10){ //4
        coupon = 15;
        offset = 20;
        base = 10;
    }
    return coupon*offset*base*value/100; //5
}
public static void main(String[] args) {
    System.out.println(calc(100));
} }
M.B
  • 49
  • 6
  • Well, what would happen if `percent` were not less than 10? What should this method return in that case? – Dawood ibn Kareem Dec 21 '20 at 04:51
  • I guess what you're saying is that the compiler ought to be able to analyze the code and see that the only outcome actually possible is for `percent` to be less than 10 and so for those other variables to be initialized. But if your code is counting on that, why have the `if` check at all? If the compiler should allow this, then it should also tell you that you should remove the `if`, because it is always `true`. – CryptoFool Dec 21 '20 at 04:59

4 Answers4

1

Allright. So, later on, I write this code:

class OtherCode {
    public static void foo() {
      Discounter.percent = 20;
      calc(20);
    }
}

To solve this, java intentionally isn't going to try to tie itself into knots trying to do deep code analysis to figure out if things are initialized or not. It applies a simple, and well-specced list of checks. They don't 'catch' that percent is always under 10 here (even though percent is not always under 10, once you toss in the face that new class files are added).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

In Java, local variables have to be always initialized explicitly before they are used. They are not given a default value as instance variables.

You have 2 options here. Either move the return statement with calculations inside the if statement.

public static double calc(double value) {
    int coupon, offset, base; // 3
    if (percent < 10) { // 4
        coupon = 15;
        offset = 20;
        base = 10;
        return coupon * offset * base * value / 100; // 5
    }
    return 0;
}

Or provide an initial value to the local variables:

int coupon = 0, offset = 0, base = 0;
D George
  • 188
  • 11
0

Your code has been formatted and rearranged differently. Some of your variables were not initialized correctly or they were misplaced.

See your new code below, then comment to see if you still need help. I am not too sure of what you are asking. Can you narrow down your question please?

package javaQuestionsAndAnswers;

public class Discounter {

public static void main(String[] args) {
    
    System.out.println("StackOverflow Problem and Solution");
    
    System.out.println(calc(100));

} 

public static double calc(double value) {
    
     double percent = 0;
     
     int coupon = 0, offset = 0, base = 0; //3
     
     if(percent <10){ //4
        coupon = 15;
        offset = 20;
            base = 10;
    
      }

    return coupon*offset*base*value/100; //5
}
}
xpqx
  • 1
  • 1
0

Java will assign default values to class/instance variables, but will not assign a default value to a local variable. Local variables must be definitely assigned in this manner (either by initialization or assignment), or a compile-time error will occur (as you observe).

ensuring coupon, offset, base initialization for every case

int coupon = 0, offset = 0, base = 0;

Mohammed Jubayer
  • 1,709
  • 1
  • 11
  • 5