2016-07-05 9 views
1

を上げました配分は、私はこのエラーを取得しています例外

public class PrimitiveCasting { 
    class anand { 
     int a = 90; 

     void anand1() { 
      System.out.println("anand is having anand1"); 
     } 
    } 

    class babu extends anand { 
     int c, b = 88; 

     void babu1() { 
      System.out.println("babu is having babu1"); 
     } 
    } 

    public static void main(String[] args) { 
     System.out.println("**********"); 
     anand z1= new anand(); 
     z1.anand1(); 

     babu b1= new babu(); 
     b1.anand1(); 
     b1.babu1();`enter code here` 
     System.out.println("********"); 
    } 

} 
+4

'anhand'のインスタンスを作成するには、内部クラスを静的にする必要があります。 (「バブ」と同じ)。さもなければ、これらのクラスは、周囲の周辺クラスのインスタンス内にのみ存在できます。 – SomeJavaGuy

+0

私は解決策を得ました。しかし、もし私が内部クラスの前に静的を追加したくなければ、どうすればいいですか? – Anand

+0

同時に私はaとbの値を加えたいと思っています。 – Anand

答えて

1

これは正常で、あなたのコードでは、あなたのアナンドクラスはPrimitiveClassのインスタンスに拘束されることを定義する、すなわち、あなたはインスタンスを持つことができますPrimitiveClassのインスタンスからのクラスを返します。それが私たちが内部クラスと呼ぶものです。 コードを動作させるには、anand(およびbabu1)クラスを静的にすることができます。ここ はコードです:

public class PrimitiveCasting { 
static class anand { 
    int a = 90; 

    void anand1() { 
     System.out.println("anand is having anand1"); 
    } 
} 

static class babu extends anand { 
    int c, b = 88; 

    void babu1() { 
     System.out.println("babu is having babu1"); 
    } 
} 

public static void main(String[] args) { 
    System.out.println("**********"); 
    anand z1= new anand(); 
    z1.anand1(); 

    babu b1= new babu(); 
    b1.anand1(); 
    b1.babu1(); 
    System.out.println("********"); 
} 

}

それとも、PrimitiveClassのインスタンスを作成し、このようなアナンドまたはバブー、インスタンス化することができます

public class PrimitiveCasting { 
class anand { 
    int a = 90; 

    void anand1() { 
     System.out.println("anand is having anand1"); 
    } 
} 

class babu extends anand { 
    int c, b = 88; 

    void babu1() { 
     System.out.println("babu is having babu1"); 
    } 
} 

public static void main(String[] args) { 
    System.out.println("**********"); 
    PrimitiveCasting primitiveCasting = new PrimitiveCasting(); 
    anand z1= primitiveCasting.new anand(); 
    z1.anand1(); 

    babu b1= primitiveCasting.new babu(); 
    b1.anand1(); 
    b1.babu1(); 
    System.out.println("********"); 
} 

}

希望これを助けて!

0

このようにインスタンスを作成する必要があります。

PrimitiveCasting pc = new PrimitiveCasting(); 
anand z1 = pc.new anand(); 

あなたのアールは、それがインスタンスタイプを囲んで修飾されなければならない静的コンテキストからanandを参照するため、という理由。

他のクラスも同様です。

0

内部クラスが静的メンバー変数ではないため、静的メソッドmainからアクセスできないため、エラーが発生しています。静的のようなものを内部クラスを作る: -

public class PrimitiveCasting { 
static class anand { 
    int a = 90; 

    void anand1() { 
     System.out.println("anand is having anand1"); 
    } 
} 

またはこれらのクラスにアクセスするには、メインメソッドでPrimitiveCastingのインスタンスを作成します -

PrimitiveCasting pc = new PrimitiveCasting(); 

    anand z1= pc.new anand(); 
    z1.anand1(); 
0

Javaプログラミング言語を使用すると、別のクラス内のクラスを定義することができます。このようなクラスはネストされたクラスと呼ばれます
静的と宣言されたネストされたクラスは静的ネストされたクラスと呼ばれます。静的でないネストされたクラスは、内部クラスと呼ばれます。
内部クラスをインスタンス化するには、まず外部クラスをインスタンス化する必要があります。次に、外側のオブジェクト内の内部オブジェクトを作成します。

OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

関連する問題