मेरा कार्यक्रम इस तरह है:
class Test {
final int x;
{
printX();
}
Test() {
System.out.println("const called");
}
void printX() {
System.out.println("Here x is " + x);
}
public static void main(String[] args) {
Test t = new Test();
}
}
अगर मैं इसे निष्पादित करने की कोशिश करता हूं, तो मुझे संकलक त्रुटि मिल रही है: variable x might not have been initializedजावा डिफ़ॉल्ट मानों के आधार पर मुझे नीचे आउटपुट सही मिलना चाहिए ??
"Here x is 0".
क्या अंतिम चर में dafault मान होंगे?
अगर मैं अपना कोड इस तरह बदलूं,
class Test {
final int x;
{
printX();
x = 7;
printX();
}
Test() {
System.out.println("const called");
}
void printX() {
System.out.println("Here x is " + x);
}
public static void main(String[] args) {
Test t = new Test();
}
}
मुझे आउटपुट मिल रहा है:
Here x is 0
Here x is 7
const called
किसी को भी इस व्यवहार की व्याख्या कर सकते हैं ..
