किसी विधि के अंदर स्थानीय आंतरिक कक्षा घोषित करते समय, अंतिम स्थिर स्ट्रिंग्स या इनट्स को शामिल करना कानूनी क्यों है लेकिन अन्य वस्तुओं को शामिल करने के लिए कानूनी नहीं है?विधि स्थानीय कक्षाओं में जावा अंतिम स्थिर घोषणा
उदाहरण के लिए:
class Outer {
void aMethod() {
class Inner {
final static String name = "compiles";
final static int ctr = 10; // compiles
final static Integer intThree = Integer.valueOf(3); // does not compile!
final static obj objConst = new Object(); // does not compile!
}
Inner inner = new Inner();
}
}
जब मैं इस संकलन, मैं निम्नलिखित हो:
InnerExample.java:6: inner classes cannot have static declarations
final static Integer outer = Integer.valueOf(3);
^
InnerExample.java:7: inner classes cannot have static declarations
final static Object objConst = new Object();
^
क्यों भेद? क्या ऐसा इसलिए है क्योंकि स्ट्रिंग अपरिवर्तनीय है? यदि ऐसा है, तो Integer.valueOf() भी मान्य नहीं होगा?
मुझे पूरा यकीन है कि यह "संकलित" है और 10 संकलन-समय निरंतर अभिव्यक्तियां हैं, लेकिन मुझे अभी तक जेएलएस नियम नहीं मिला है। –