Ask Question
13 March, 19:02

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

+5
Answers (1)
  1. 13 March, 19: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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers