11.0k views
1 vote
What is the output

public class Test {
public static void main(String args[]) {
final int i = 10;
i = 30; // Error because i is final.
}
}

1 Answer

3 votes

Final answer:

The student's Java code will result in a compilation error because the final variable 'i' is attempted to be reassigned a new value, which is not allowed.

Step-by-step explanation:

The student is asking about what output the following Java code would produce:

public class Test {
public static void main(String args[]) {
final int i = 10;
i = 30; // Error because i is final.
}
}

In this code snippet, the final keyword is used when declaring the variable i. The keyword final in Java is used to declare a constant, which means that once a value is assigned to the variable, it cannot be changed. In the given code, an attempt is made to reassign a value to i, which will result in a compilation error because i is final and cannot be reassigned.

The code snippet provided is written in the Java programming language. It demonstrates the use of the 'final' keyword, which is used to declare a constant variable that cannot be modified once assigned a value. In the given code, the variable 'i' is declared as final and assigned the value 10.

User Vemula Abilash
by
8.7k points