Android & Java inner class concept
Asked Answered
E

5

6

i followed the link http://developer.android.com/reference/android/app/AlertDialog.html and i try to create new AlertDialog like this

AlertDialog myAlertDialog = new AlertDialog.Builder(MainActivity.this).create();

as per the document AlerDialog is the outerclass and Builder is the inner class within AlertDialog. Now i linked the same concept with java in accessing the inner class like this Outer myOuter2 = new Outer.Inner(); this piece of gives error when i try to access, here is the complete java code

package com.test;

    public class Outer {
        public void OuterMethod() {
            System.out.println("OuterMethod");
        }

        public static void main(String[] args) {
            Outer myOuter = new Outer();

            myOuter.OuterMethod();
            Outer myOuter2 = new Outer.Inner();//this piece of code gives error

        }

        class Inner {

            Inner() {
                System.out.println("constructor Inner");
            }

            public void InnerMethod() {
                System.out.println("Inside InnerMethod");
            }
        }
    }

so my question over here is how to understand the same inner class concept in android and accessing the methods within that

Ellerd answered 25/6, 2013 at 13:47 Comment(2)
What does the error say?Yearling
Make class Inner static + Duncan's answer.Pulsometer
F
14

You have created an inner non-static class (an inner instance class), whereas AlertDialog.Builder is a static class.

To get your code to work as is you need an interesting way of invoking new that goes like this:

Outer.Inner myOuter2 = myOuter.new Inner();

This is because it acts much like any other non-static field within Outer - it requires an instance of Outer in order to be valid. In any event, this is often not a good idea as public inner non-static classes are rare.

More likely you want Inner to be a static class, i.e. one declared as:

static class Inner {

Essentially this decouples Inner from its containing class, it just happens to live inside it and so can be instantiated via new Outer.Inner(). It could happily live as a public class in its own right in a new .java file instead.

Inner static classes are useful when the inner class is only used in relation the outer class, so it shows the relationship between them.

In Android's case you use an AlertDialog.Builder only when building an AlertDialog. If it was a general Builder used by other classes (e.g. a plain Dialog) is would have instead been declared as its own public class (i.e. a standalone class that is not nested inside another).

Fillmore answered 25/6, 2013 at 13:57 Comment(0)
P
6

There is no relationship between Outer and Inner except that they share a class file. Hence, you cannot type:

Outer myOuter2 = new Outer.Inner();

Perhaps you meant:

Outer.Inner myInner = new Outer.Inner();

The Inner class will need to be declared as static for this to work.


Note that a normal builder will return a type that is equal to the enclosing type. Here's a small example using similar class names to your code:

public class Outer {

  public static void main(String[] args) {
    Outer outer = new Outer.Builder().withParam("foo").build();
  }

  private final String someParam;

  private Outer(String someParam) {
    this.someParam = someParam;
  }

  public static class Builder {

    private String someParam;

    public Builder() {
    }

    public Builder withParam(String value) {
      this.someParam = value;
      return this;
    }

    public Outer build() {
      return new Outer(someParam);
    }
  }
}

You may also wish to read Item #2 of Joshua Bloch's Effective Java, 2nd Edition for a good description of builder design and rationale. Available online: here.

Paulpaula answered 25/6, 2013 at 13:49 Comment(2)
That only takes care of one problem.Yearling
Inner needs to be a static inner class.Pulsometer
T
1

Your inner class is non static type. We should first create instance of your outer class:

Outer o=new Outer();
Outer.Inner oi=o.new Inner();

This is the basic way of create non static inner class object.

Suppose if your inner is of type static (i.e. static class Inner{....}), then for creating object:

Outer.Inner oi=new Outer.inner();
Tocsin answered 22/7, 2018 at 7:24 Comment(0)
C
0

The AlertDialog.Builder class is a static inner class as you can see here.

public static class Builder {...}
Collinear answered 25/6, 2013 at 13:51 Comment(0)
E
0

Finally i figured out here is the code

package com.test;

public class Outer {
    public void OuterMethod() {
        System.out.println("OuterMethod");
    }

    public static void main(String[] args) {
        Outer myOuter = new Outer();

        myOuter.OuterMethod();
        Outer myOuter2 = new Outer.Inner().InnerMethod();
    }

    static class Inner {

        Inner() {
            System.out.println("constructor Inner");
        }

        public Outer InnerMethod() {
            Outer myOuter = new Outer();
            System.out.println("Inside InnerMethod");
            return myOuter;

        }
    }
}
Ellerd answered 26/6, 2013 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.