Ask 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 = = 7) return 1;

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

+1
Answers (1)
  1. 14 March, 11:55
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers