Lambda Function in Java
What are you going to learn from this blog?
- Create a class inside another class and learn how to access its methods.
- How to write an anonymous class in Java.
- How to write a lambda function.
Part 1 : Class inside class.
Yes, it’s kind of wierd that you can create class inside a class in java(idk about other languages T-T).
Let’s see this with an example :
class A {
void show(){
System.out.println(“show method”);
}
//creating class B inside A
class B{
void disp(){
System.out.println(“display”);
}
}
}
Now that we have created class inside a class, how de we access them?
First, we create an object of A. Since B is inside A an object of A is required to access B.
A a = new A();
This is how you can access B :
A.B b = a.new B();
Common Raj what if we don’t want to create object of A what do we do?
Well, just make the class static it’s that simple :)
class A {
void show(){
System.out.println(“show method”);
}
//creating class B inside A
static class B{
void disp(){
Get Raj Dhok’s stories in your inbox
Join Medium for free to get updates from this writer.
System.out.println(“display”);
}
}
}
Syntax for how to access static class B :
A.B b = new A.B();
Part 2 : Anonymous class in Java.
An anonymous class is created during the creation of an object, not during the creation of a class.
We have class A(Parent class) and B(child class) which extends A and also override the show() method.
While creating the object of A we can see that the show() method is overridden in those curly braces ‘{}’. This is nothing but anonymous class.
What do you thing which method will get called?
First take a look at the code :
class A{
void show(){
System.out.println(“show A”);
}
}
class B extends A{
void show(){
System.out.println(“show B”);
}
}
class Main {
public static void main(String[] args) {
A a = new A(){
void show(){
System.out.println(“show anonymous”);
}
};
a.show(); // o/p : show anonymous
}
}
Part 3 : Lambda function.
Lambda is a replacement only for anonymous classes that implement functional interface to increase the redability of code. Let’s understand with example.
interface A{
void show();
}
class Main {
public static void main(String[] args) {
A a = ()-> System.out.println(“show method here”);
a.show();
}
Lambda expression provides an implementation of the interface’s abstract method.
What if multiple (more than one) methods are there?
It won’t work if the interface have more than one abstract class.Lambda expressions work only with functional interfaces (interfaces having exactly one abstract method).
@FunctionalInterface is optional but recommended. For multiple method Use Anonymous Class.
example with Functional Interface:
@FunctionalInterface
interface A {
void show();
}
What if the method have parameters?
interface A{
int add(int a, int b);
}
class B implements A{
public int add(int a,int b){
return a+b;
}
}
class Main {
public static void main(String[] args) {
A obj = (a,b) -> a+b;
System.out.println(obj.add(4,6));
}
}
Well, it’s as simple as that.
We can also write it like this :
A obj = (int a, int b) -> { return a+b ;};
But the first one looks much easies to understand and as the type of variables are already defined we don’t need to specify them as well and also don’t need to ‘return’. All this is just syntactic sugar (better life for a programmer uwu).
Also note that anonymous class creates a new class and lambda function does not create a new class.








