निम्नलिखित संकलन नहीं करता है, एक 'अवैध आगे संदर्भ' संदेश दे रही है:दो प्रोग्रामों में त्रुटियों का संदर्भ देने में आगे क्यों है जबकि तीसरा नहीं है?
class StaticInitialisation {
static
{
System.out.println("Test string is: " + testString);
}
private static String testString;
public static void main(String args[]) {
new StaticInitialisation();
}
}
हालांकि, निम्नलिखित संकलन करता है:
class InstanceInitialisation1 {
{
System.out.println("Test string is: " + this.testString);
}
private String testString;
public static void main(String args[]) {
new InstanceInitialisation1();
}
}
लेकिन निम्नलिखित संकलन नहीं करता है, एक 'अवैध आगे दे रही है संदर्भ 'संदेश:
class InstanceInitialisation2 {
private String testString1;
{
testString1 = testString2;
}
private String testString2;
public static void main(String args[]) {
new InstanceInitialisation2();
}
}
क्यों StaticInitialisation और InstanceInitialisation2 संकलन नहीं है, जबकि InstanceInitialisation1 हिरण रों?
देखने के लिए कि करने के लिए बदल 3 टुकड़ा काम करेगा 'testString1 = this.testString2;' – Eran
इसका पहली बार मैं एक जावा वर्ग में कोड का एक खंड देखना है कि एक ctor का हिस्सा नहीं है, या स्थैतिक सीटीओ, या विधि। यह जानना अच्छा लगेगा कि यह क्या है, यह संकलन कैसे पास करता है और इसे कब –
@sharonbn निष्पादित किया जाता है: वे उदाहरण प्रारंभकर्ता या स्थैतिक प्रारंभकर्ता हैं, क्रमशः जेएलएस की धारा 8.6 और 8.7 में वर्णित हैं। –