×

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a type of nested function (non-local functions). Therefore, closures must be used to implement anonymous functions. Lambda is only an anonymous function that may be passed around in an efficient manner.

An anonymous function is a definition as a function that is not associated with an identifier. Examples include function literals, lambda abstractions, lambda functions, and lambda expressions or blocks. For higher-order functions that need to return a function, anonymous functions are frequently utilized as inputs or to build the result.

An anonymous function is represented by a lambda expression, which also includes a function body, a set of arguments, and the lambda operator (->).

Return Type

The anonymous function's return type is the same as the body expression when there is only one statement.

The return type of an anonymous method is identical to the kind of result returned inside the code block when more than a statement is surrounded in curly brackets, or void when nothing is returned.

Examples 

The following are some examples:

Zero parameter

 () ->System.out.println(" No Parameters ");

one parameter

System.out.println("One parameter is: " + parameter); or

omitting the parentheses; parameter -> parameter (similar function example)

multiple parameters

(parameter1, parameter2) -> parameter1 + parameter2;

Types of Parameters

(int a, String name) ->System.out.println("id:" + (int a + a + "name:" + name);

Functional Interfaces

Let's assume that in our project we are able to identify all single-task APIs, or APIs with a single task to fulfill. like the Adder class in our programming language, which merely has to add two values and output the result. We, therefore, identify all such activities and make them accessible via a Java interface:

public interface AdderInterface {
   int addNumbers(int a, int b);
}

And afterward, a class that implements it, like Adder, can do so. Such single-task interfaces do, however, have a specific place in Java, and since the release of JDK 8, their use has increased. We'll come to these eventually, but it's important to note that even JDK contains examples of single-tasked interfaces, such as java.lang.Runnable Interface.

Functional interfaces are what these interfaces are known as. Interfaces having just one abstract method included are said to be functional interfaces. And the "functionality" of this interface would be specified by this abstract method. The default methods stated with the standard keyword are non-abstract methods that can be found in a functional interface. In the event that the implementing class doesn't really offer any, these default methods give the interface's default implementations!

A functional interface can now be implemented in two different ways. The first step would be to develop a distinct class, like Adder, that would support the functional interface; alternatively, we might do so invisibly!

Anonymous Classes

Typically, anonymous classes implement interfaces or extend subclasses.

Type may be used here

  • an anonymous class that extends a superclass
  • a class that implements an anonymous interface

The code aforementioned dynamically constructs object1, an object of an anonymous class. An expression contains the definition of anonymous classes. Therefore, the semicolon is used to denote the end of the expression at the end of unnamed classes.

Recall that we mentioned the dynamic languages issue; to avoid it, we might implement the interface in an anonymous manner.

AdderInterfaceaddrInterface= newAdderInterface ()
{
Public int addNumbers (int a,int b){
                   return a+b;
                                  }
                               };

Now that this addrInterface contains an anonymous representation of the initial AdderInterface, it may be used just like a conventional AdderInterface implementation with a named implementation! Be aware that we did not develop a concrete class that implements the AdderInterface interface in the aforementioned implementation. By implementing this, we have already significantly lessened boilerplate and verbosity.

There are also finer distinctions to be made in addition to the reduced boilerplate and verbosity, though. Take a look at the anonymous approach once more, but this time with the exception of the fact that it is now contained within a class with its own member var, let's say outer, and the anonymous approach also has a state - inner:

class Encapsulator{
  int outer = 50;
AdderInterfaceaddrInterface = new AdderInterface(){
  int inner = 30;
 public int addNumbers (int a, int b){
int in = this.inner; 
Encapsulator.this.outer
return a+b;
 }
};
}

While the instance variables inner is available within the anonymous implementation of the addNumbers(...)method, the instance variable outer, which is a member of the enclosing class, is not. Instead, we must use the following construct to access outer within the anonymous implementation: Encapsulator.this.outer

Example 1

class Polygon {
   public void display() {
System.out.println("Inside the Polygon class");
   }
}


class AnonymousDemo {
   public void createClass() {


      Polygon p1 = new Polygon() {
         public void display() {
System.out.println("Inside an anonymous class.");
         }
      };
      p1.display();
   }
}


class Main {
   public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
   }
}

The output of the above program

Inside an anonymous class.

Example 2

interface Polygon {
   public void display();
}


class AnonymousDemo {
   public void createClass() {
      Polygon p1 = new Polygon() {
         public void display() {
System.out.println("Inside an anonymous class.");
         }
      };
      p1.display();
   }
}


class Main {
   public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
   }
}

The output of the above program

Inside an anonymous class.

Benefits of Anonymized Classes

Whenever they are needed, objects are created in anonymous classes. In other words, objects are made to carry out some specific functions. For instance,

Object = new Example() {
 public void display() {
System.out.println("Anonymous class overrides the method display().");
}
}

When we want to override its display() method, in this case, a dynamically constructed object of an anonymous class is created.

We can write cleaner code by using anonymous classes.


Related Topics

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.