2012-02-22 19 views
6
public class Array                
{ 
    static String[] a = new String[] {"red", "green", "blue"}; 
    static Point[] p = new Point[] {new Point(1, 2), "3,4"}; 

    public static void main(String[] args) 
    { 
     System.out.println("hello"); 
    } 

    class Point 
    { 
     int x; 
     int y; 

     Point(int x, int y) 
     { 
      this.x = x; 
      this.y = y; 
     } 

     Point(String s) 
     { 
      String[] a = s.split(","); 
      x = a[0].parseInt(); 
      y = a[1].parseInt(); 
     } 
    } 
} 

उपरोक्त कार्यक्रम में, स्थिर Point सरणी प्रारंभ विफल रहता है, रिपोर्टिंग त्रुटि:स्थिर वस्तु सरणी

Array.java:4: non-static variable this cannot be referenced from a static context 
    static Point[] p = new Point[] {new Point(1, 2), "3,4"}; 

लेकिन, स्थिर String सरणी सफल होता है। उनके बीच क्या अंतर है?

मुझे वास्तव में एक स्थिर ऑब्जेक्ट सरणी की आवश्यकता है, क्योंकि बाह्य वर्ग को तुरंत बिना संदर्भित करना आसान है।

धन्यवाद

उत्तर

4

आप Point एक स्थिर नेस्टेड वर्ग बनाने के लिए, इस तरह आवश्यकता होगी:

static class Point { 
1

Point[] सरणी होना चाहिए या Point"3,4"Point के कहने और प्वाइंट वर्ग के लिए स्थिर नहीं है यह त्रुटि

1

यहां Oracle Online Reference

से स्पष्टीकरण दिया गया है

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass { 
    ... 
    class InnerClass { 
     ... 
    } 
} 

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

यह भी, संलग्न प्रकार के स्थिर सदस्यों के भीतर से पहुँचा नहीं जा सकता, क्योंकि यह अस्तित्व के लिए एक एक उदाहरण की आवश्यकता है।

1

स्थिर के रूप में अपने Point वर्ग घोषित:

... 
static class Point 
{ 
    ... 
} 

यह क्योंकि Point एक आंतरिक वर्ग (String नहीं है) है की जरूरत है।

हालांकि, {new Point(1, 2), "3,4"}; आपकी अगली समस्या है। मुझे लगता है कि यह {new Point(1, 2), Point(3, 4)};

+0

धन्यवाद। मैं प्वाइंट (स्ट्रिंग) को ट्रिगर करने के लिए "3,4" चाहता हूं। ऐसा लगता है कि ऐसा करने में असमर्थ है। – pengguang001

+0

'प्वाइंट (स्ट्रिंग xy) {स्ट्रिंग [] tmp = xy.split (", "); एक्स = Integer.parseInt (टीएमपी [0]); वाई = Integer.parseInt (टीएमपी [1]); } '? – khachik

0

हो सकता है कि आप प्वाइंट स्टेटिक नेस्टेड क्लास बनाना चाहें।

static class Point { 

} 

जांच this

5

आप तीन बातें अपने कोड काम करने के लिए बनाने के लिए क्या करना है। मैं उन्हें समझाऊंगा। सबसे पहले वर्किंग वर्जन देखें।

public class Array { 
    static String[] a = new String[]{"red", "green", "blue"}; 
    static Point[] p = new Point[]{new Point(1, 2), new Point("3,4")}; 

    public static void main(String[] args) { 
     System.out.println("hello"); 
    } 

    static class Point { 
     int x; 
     int y; 

     Point(int x, int y) { 
      this.x = x; 
      this.y = y; 
     } 

     Point(String s) { 
      String[] a = s.split(","); 
      x = Integer.parseInt(a[0]); 
      y = Integer.parseInt(a[1]); 
     } 
    } 
} 

निम्नलिखित तीन परिवर्तन हैं जो आपको करना है।

1. परिवर्तन "3,4"new Point("3,4") करने के लिए या new Point(3,4)

हम जानते हैं कि एक सरणी समान प्रकार के आइटम पकड़ कर सकते हैं। यहां आप p नाम Point नामक एक सरणी घोषित कर रहे हैं। इसका मतलब है कि इसमें केवल Point प्रकार (या इसके उप प्रकार) की वस्तु हो सकती है। लेकिन दूसरा तत्व "3,4" टाइप String है और आपके पास मेल नहीं है। इसलिए आपको Point टाइप करने के लिए new Point("3,4") या new Point(3,4) निर्दिष्ट करना होगा।

2. आप अपने Point वर्ग static

से Java Tutorial बनाने की जरूरत है:

An instance of InnerClass can exist only within an instance of OuterClass 
and has direct access to the methods and fields of its enclosing instance. 

यहाँ अपने Point वर्ग एक आंतरिक वर्ग है और यह Array के सभी सदस्यों को जानकारी दे दी गयी है चाहिए कक्षा। इसके लिए, Point वर्ग की प्रत्येक वस्तु Array कक्षा के किसी ऑब्जेक्ट से जुड़ी होनी चाहिए। लेकिन, आप बना रहे हैं pstatic संदर्भ में है। इसलिए, आपको या तो Point कक्षा static एक बनाना है या सरणी p एक गैर स्थैतिक बनाना है।

3. parseIntString वर्ग की एक विधि

parseIntInteger वर्ग के एक स्थिर विधि और String वर्ग की नहीं है नहीं है। तो आपको इसे Integer.parseInt(stringValue) पर कॉल करना होगा।

उम्मीद है कि यह मदद करता है :)

+0

आपकी तरह की याद दिलाने के लिए धन्यवाद। अब मेरा कार्यक्रम काम करता है। – pengguang001

संबंधित मुद्दे