JAVA Programming :: Basic Concepts - Discussion
34 / 64
public class A {
int k;
boolean istrue;
static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test{
public static void main(String argv[]){
A a = new A();
printValue();
}
}
public class A {
int k;
boolean istrue;
static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test{
public static void main(String argv[]){
A a = new A();
printValue();
}
}
A0 false 0
B0 true 0
C0 0 0
DCompile error - static variable must be initialized before use.
Show Explanation
Asked In ::
The above code will produce error, as printValue() method is a non-static method, and has to be invoked by an object of the class. It should have been been invoked as a.printValue();
After you correct it, it will produce the output as 0 false 0.
The reason is that k is a global variable or you can say instance variable, and p is a static int variable, and note that global and static int variables need not be initialized before use. The default avlue for both is 0. On the other hand, istrue is a boolean variable, and default value of boolean variable is false.
Hence, the output is 0 false 0.
Read Full Answer
Report Error
Please Login First Click Here