A Java nested class is a class within a class. You may want to create a class within a class for data encapsulation and protect that class from calling from other classes outside.
package com.javapointers.javacore; public class ClassA { public static void main(String[] args) { ClassB b = new ClassB(); int ans = b.addNum(10,20); System.out.println(ans); } } class ClassB { public int addNum(int first, int second) { int answer = first + second; return answer; } }
package com.javapointers.javacore; public class ClassA extends ClassB{ public static void main(String args[]){ ClassA a = new ClassA(); String con = a.concatString("Hello ", "Javapointers"); System.out.println(con); } } class ClassB { public String concatString(String a, String b){ String con = a.concat(b); return con; } }
The next topic is about how to return value from a method in Java.