What is the output of the following Java program?

Category: JavaWhat is the output of the following Java program?
Editor">Editor Staff asked 2 years ago

What is the output of the following Java program?
class Test
{
int i;
}
public class Main
{
public static void main (String args[])
{
Test test = new Test();
System.out.println(test.i);
}
}

1 Answers
Editor">Editor Staff answered 2 years ago

The output of the program is 0 because the variable i is initialized to 0 internally. As we know that a default constructor is invoked implicitly if there is no constructor in the class, the
variable i is initialized to 0 since there is no constructor in the class.