0% found this document useful (0 votes)
60 views3 pages

An Example of A Simple Anonymous Class

The document provides examples of using anonymous inner classes in Java. It demonstrates how to define and instantiate anonymous classes, pass arguments to anonymous class constructors, and return anonymous classes from methods. Key points covered include initializing fields, requiring arguments be final if used inside the anonymous class, and needing a semicolon after returning an anonymous class instance.

Uploaded by

faruk409
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views3 pages

An Example of A Simple Anonymous Class

The document provides examples of using anonymous inner classes in Java. It demonstrates how to define and instantiate anonymous classes, pass arguments to anonymous class constructors, and return anonymous classes from methods. Key points covered include initializing fields, requiring arguments be final if used inside the anonymous class, and needing a semicolon after returning an anonymous class instance.

Uploaded by

faruk409
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

an example of a simple anonymous class

   

public class MainClass {
  public static void main(String[] args) {
    Ball b = new Ball() {
      public void hit() {
        System.out.println("You hit it!");
      }
    };
    b.hit();
  }

  interface Ball {
    void hit();
  }
}

 Creating a constructor for an anonymous inner class

abstract class Base {
  public Base(int i) {
    System.out.println("Base constructor, i = " + i);
  }

  public abstract void f();
}

public class MainClass {
  public static Base getBase(int i) {
    return new Base(i) {
      {
        System.out.println("Inside instance initializer");
      }

      public void f() {
        System.out.println("In anonymous f()");
      }
    };
  }

  public static void main(String[] args) {
    Base base = getBase(47);
    base.f();
  }
}
Base constructor, i = 47
Inside instance initializer
In anonymous f()

Argument must be final to use inside anonymous inner class


public class MainClass {
  public A dest(final String dest) {
    return new A() {
      private String label = dest;

      public String readLabel() {
        return label;
      }
    };
  }

  public static void main(String[] args) {
    MainClass p = new MainClass();
    A d = p.dest("A");
  }
}

interface A {
  String readLabel();
}

A method that returns an anonymous inner class

public class MainClass {
  public A cont() {
    return new A() {
      private int i = 11;

      public int value() {
        return i;
      }
    }; // Semicolon required in this case
  }

  public static void main(String[] args) {
    MainClass p = new MainClass();
    A c = p.cont();
  }
}

interface A {
  int value();
}

11. An anonymous inner class that performs initialization

public class MainClass {
  public A dest(final String dest) {
    return new A() {
      private String label = dest;
      public String readLabel() {
        return label;
      }
    };
  }

  public static void main(String[] args) {
    MainClass p = new MainClass();
    A d = p.dest("A");
  }
}

interface A {
  String readLabel();
}

You might also like