Java find output programs (final Keyword) | set 1

Find the output of Java programs | final Keyword | Set 1: Enhance the knowledge of Java final Keyword concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 02, 2021

Question 1:

public class Main {
  final static int X = 10;

  public static void main(String[] args) {
    int Z;
    int Y;

    X = 20;
    Y = 30;

    Z = X + Y;

    System.out.println(Z);
  }
}

Output:

Main.java:8: error: cannot assign a value to final variable X
    X = 20;
    ^
1 error

Explanation:

The above program will generate syntax error because here we created a variable X initialized with 10. And we changed the value of X in the main() function but we cannot change the value of the final variable because the final keyword is used to create constants in Java.

Question 2:

public class FinalEx {
  final int X;

  FinalEx() {
    X = 10;
  }
  public static void main(String[] args) {
    int Z = 0;
    int Y = 20;

    FinalEx F = new FinalEx();

    Z = F.X + Y;

    System.out.println(Z);
  }
}

Output:

30

Explanation:

In the above program, we created a class FinalEx that contains a data member X using the final, and we initialized X with 10 in the constructor.

Now look to the main() method, here we created two local variables Y and Z initialized with 0 and 20 respectively, and we created the object of FinalEx class.

Now look to the below expression,

Z = F.X + Y;
Z = 10 + 20;
Z = 30;

And then finally we print the value of Z on the console screen.

Question 3:

public class FinalEx {
  final static int X;

  public static void main(String[] args) {
    int Z;
    int Y = 20;

    Z = X + Y;

    System.out.println(Z);
  }
}

Output:

/FinalEx.java:2: error: variable X not initialized in the default constructor
  final static int X;
                   ^
1 error

Explanation:

The above program will generate syntax error because we cannot create an uninitialized final variable in Java.

Question 4:

public class FinalEx {
  final int X;

  final FinalEx() {
    X = 10;
  }
  public static void main(String[] args) {
    int Z;
    int Y = 20;

    FinalEx F = new FinalEx();

    Z = F.X + Y;

    System.out.println(Z);
  }
}

Output:

/FinalEx.java:4: error: modifier final not allowed here
  final FinalEx() {
        ^
1 error

Explanation:

The above program will generate syntax error because we cannot use the final keyword with the constructor of the class.

Question 5:

final class Sample {
  static void sayHello() {
    System.out.println("Hello World");
  }
}

public class Main: Sample {
  public static void main(String[] args) {
    sayHello();
  }
}

Output:

FinalEx.java:7: error: '{' expected
public class Main: Sample {
                 ^
1 error

Explanation:

The above program will generate syntax error because here we use a colon ":" operator to inherit Sample class into Main. We need to use the extends keyword for inheritance in Java.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.