简体   繁体   中英

inner class and how to access them

I have the following code with a nested class called out1

 class sample{
    public int a=5;

    class out1{

        void main1(){
            System.out.println("this is out1");
        }

    }

    void call(){
        //access main1() method on class out1 
    }       

}

public class innerclass{
    public static void main(String args[]){
        sample ob=new sample();
        System.out.println(ob.a);// access field a on class sample 

        //access call() on class sample

}   
 }

does anyone know on how to access inner class out1 and is it possible to access this inner class without using call() method on class sample?

您可以创建内部类out1对象为

 ob.new out1();

This is my way how to access inner classes. I made a get method in class sample which returns an object of class out1:

public class innerclass {

public static void main(String args[]) {
    sample ob = new sample();
    ob.getOut1().call(); // calling the call() method in innerclass out1
}
}



class sample {

public int a = 5;

out1 getOut1() {
    return new out1();
}

class out1 {

    public void main1() {
        System.out.println("this is out1");
    }

    public void call() {
        main1();
    }
}
}

And try to make classes with uppercase letter and also use camelcase like: Sample, InnerClass, Out1.

Inner classes can be static.
If you do not define your inner class as static, then you have to create an instance of it (an object) in order to use it.

Here is an example use case of static and member (non static) classes:

public class Tester {

    public static void main() {
        Outer outerTest = new Outer();
        outerTest.test();
        outerTest.publicInnerInstance.sayHello();
        Outer.InnerStaticClass.sayHello();
    }

}

class Outer{

    class InnerMemberClass{
        public void sayHello(){
            System.out.println("Hello");
            System.out.println("I'm an instance of 'InnerMemberClass'.");
        }
    }

    static class InnerStaticClass{
        public static void sayHello(){
            System.out.println("Hello.");
            System.out.println("I'm a static class 'InnerStaticClass'.");
        }
    }

    public InnerMemberClass publicInnerInstance;

    //'Outer' constructor
    public void Outer(){
        publicInnerInstance = new InnerMemberClass();
    }

    public void test(){
        InnerStaticClass.sayHello();
        InnerMemberClass instance = new InnerMemberClass();
        instance.sayHello();
    }
}

You can access the innerclass by new of outer.new of inner. To call inner class method from outer class you need to create an object of the inner class.Otherwise you have to make the inner class as well as the method static.

class Sample{
public int a=5;

class Out1{

    void main1(){
        System.out.println("this is out1");
    }

}

void call(){
    new Out1().main1();
}       

}

public class Innerclass{
public static void main(String args[]){
    Sample ob=new Sample();
    System.out.println(ob.a);// access field a on class sample
    Sample.Out1 out1=new Sample().new Out1();
    Out1.main1();
    ob.call();
    //access call() on class sample

}   
}

And class names should start with capital letter by convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM