Ask Question
25 August, 11:43

What is wrong with the following code? How should it be fixed?

1 public class H2ClassH {

2 final int x;

3

4. int H2ClassH () {

5 if (x = = 7) return 1;

6 return 2; 7 } / / end 8 } / / end class H2ClassH

+1
Answers (1)
  1. 25 August, 12:46
    0
    Final variable x is not initialized.

    Explanation:

    In this JAVA code we have a final variable named x. The thing with final variable is that they act like constants in C++. Once initialized cannot be changed after that and we have to initialize them at the time of declaration.

    In JAVA if we do not initialize the final variable the compiler will throw compile time error. Final variables are only initialized once.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “What is wrong with the following code? How should it be fixed? 1 public class H2ClassH { 2 final int x; 3 4. int H2ClassH () { 5 if (x = = ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers