Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android & Java inner class concept

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

like image 304
User2364902 Avatar asked Jun 25 '13 13:06

User2364902


People also ask

What is meant of Android?

Definition of android : a mobile robot usually with a human form sci-fi androids.

Which is better Apple or Android?

Whether iOS is better than Android in security is now up for debate, but the consensus still gives Apple the upper hand. iOS has more consistent updates for all devices, a closed ecosystem that is harder to penetrate, and a stricter app store.

Why are androids better than iphones?

You Want More Freedom, Control, and Customizations. Since Android is an open-source platform, it offers more freedom and customization options than iOS. You can customize almost anything on your Android device, from how your home screen looks to how you interact with your phone.


2 Answers

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).

like image 109
Motti Strom Avatar answered Oct 24 '22 13:10

Motti Strom


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.

like image 40
Duncan Jones Avatar answered Oct 24 '22 12:10

Duncan Jones