¿Cómo crear un objeto de subclase en Java?

I am trying to create an object of subclass which is not static in the main, but not able to do. Does anyone tried the same?

package com.example;

import com.example.SubClassExample.MainClass.SubClass;

public class SubClassExample {

    static class MainClass {
        public int mca;
        protected int mcb;
        private int mcc;

        SubClass sc = new SubClass();

        public class SubClass {
            public int sca;
            protected int scb;
            private int scc;

            void method() {
                mca = 10;
                mcb = 20;
                mcc = 30;

                sca = 10;
                scb = 20;
                scc = 30;

                System.out.println("Subclass: " + mca + " " + mcb + " " + mcc + " ");
                System.out.println("Subclass: " + sca + " " + scb + " " + scc + " ");
            }

        }

        void method() {
            mca = 100;
            mcb = 200;
            mcc = 300;

            sc.sca = 100;
            sc.scb = 200;
            sc.scc = 300;

            System.out.println("MainClass: " + mca + " " + mcb + " " + mcc + " ");
            System.out.println("MainClass: " + sc.sca + " " + sc.scb + " " + sc.scc + " ");
        }
    }

    public static void main(String[] args) {

        MainClass obj = new MainClass();
        obj.method();
        obj.sc.method();

        SubClass obj1 = new obj.SubClass(); //Getting ERROR here, tried so many like
                    //MainClass.clas.SubClass, obj.class.SubClass, Subclass() but still not able 
                    //to do. As it is a public subclass, cant we able to create an object of 
                    //that class
        obj1.method();
    }

}

preguntado el 27 de noviembre de 13 a las 07:11

4 Respuestas

Subclass instance = new MainClass().new SubClass();

o en tu caso

Subclass instance = obj.new SubClass();

You need the instance of the parent class to create instance of inner non-static class.

respondido 27 nov., 13:07

In the above code obj is already instance of MainClass(), but still not able to do, any reason ? - Suman

Thanks got it :), SubClass obj1 = obj.new SubClass(); - Suman

Ningún problema. Encantado de ayudar - Narendra Pathai

@Suman thanks for the comment I got it too :) - Pan de jengibre

As SubClass is not static so you need to create new instance.

MainClass obj = new MainClass();
SubClass obj1 = obj.new SubClass();

respondido 27 nov., 13:07

SubClass obj1 = new obj.SubClass(); //Getting ERROR here, tried so many like

No puedes poner un new antes de obj:instance of MyClass. It actually doesn't make sense. This is because the object of the inner class is quietly connected to the object of the outer class that it was made from. A declaring obj.new InnerClass() has the same equivalent meaning of obj.innerInstance: referencing the new inner instance by the instance of outer class.

That is why, this line should be: SubClass obj1 = obj.new SubClass();

respondido 27 nov., 13:07

why you made main class static and in my code i must make Widow class static to make code run "

public class WoodenProducts {
/*
String treeType;
 int treeAge;

 */
double meterPrice;

 public WoodenProducts(){
     meterPrice = 100;

}


 class Widow extends WoodenProducts{

}


public static void main(String[] args) {

    Widow W = new WoodenProducts.Widow();

    System.out.println(" meter price is " + W.meterPrice);


}

} "

respondido 17 nov., 21:18

Tal como está escrito actualmente, su respuesta no está clara. Por favor editar para agregar detalles adicionales que ayudarán a otros a comprender cómo esto aborda la pregunta formulada. Puede encontrar más información sobre cómo escribir buenas respuestas. en el centro de ayuda. - Comunidad

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.