Edi
Edi

Reputation: 73

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?

Upvotes: 0

Views: 142

Answers (4)

EyalAr
EyalAr

Reputation: 3170

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();
    }
}

Upvotes: 0

vishnu viswanath
vishnu viswanath

Reputation: 3854

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.

Upvotes: 0

Klemenko
Klemenko

Reputation: 724

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.

Upvotes: 0

Dungeon Hunter
Dungeon Hunter

Reputation: 20593

You can create inner class out1 object as

 ob.new out1();

Upvotes: 1

Related Questions