- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to show the Nesting of Methods
Nesting of methods is a hybrid function calling method in Java, which can call another method in the same class. There are two types of nested classes are available in a Java environment.
Non-static nested class (also known as , the inner class)
Static nested class
A non-static nested class (or, inner class) is a defined class within a particular class. It also contains some outer classes with some access authorities. In this method, we can use "." operator to create the instance of the inner class by using an outer class.
On the other hand; a static nested class is a particular class created inside a class. But this type of nested class cannot access any methods and members, which are non-static in nature. They are can be accessed by an outer class.
Here in this article today, we will learn about the various nesting methods present in a Java Virtual Machine environment. And with them we will write some possible codes with the mentioned syntax and algorithm below.
Algorithm to show the nesting method in Java
In this possible algorithm we will learn about how a nesting method really works in a Java environment. By following these steps we will build some Java codes according the problem statement.
Step 1 − Start.
Step 2 − Input data samples.
Step 3 − Initialize the input weights.
Step 4 − Initialize the biases of hidden nodes.
Step 5 − Select a function to define.
Step 6 − If, the method satisfies the logic then go forward.
Step 7 − Else, go back to Step three and four again.
Step 8 − If, the method satisfies the code, select optimal input weight and biases of hidden nodes.
Step 9 − Determine the initial number of hidden nodes by applying empirical method.
Step 10 − Determine optimal number of hidden nodes.
Step 11 − Train the sample.
Step 12 − If, the accuracy mark reached then terminate the process.
Step 13 − Obtain nested model.
Step 14 − Make all predictions.
Step 15 − Else, again go to determine the initial number of hidden nodes by applying empirical method.
Syntax to show the nesting of methods
Process Syntax:
class Main { method1(){ } method2(){ method1(); } method3(){ method2(); } }
Class Syntax:
class OuterClass { // ... class NestedClass { // ... } }
Nested Method With Java Syntax:
class Nesting2001{ int m, n; Nesting2001 (int x, int y){ m=x; n=y; } int largest ( ){ if (m >=n) return(m); else return(n); } void display( ){ int large=largest ( ); System.out. println("largest value is here" large); } } class Nestmain2022{ public static void main ( String args[ ]){ Nesting2001 nest=new nesting2001 (10, 20); nest. Display( ); } }
Here in this possible syntax, we have tried to demonstrate how we can build a Java code to explain and show the various nesting methods.
Approach to show various nesting methods
Approach 1 − Find the area of a sphere by using nesting method with Java
Approach 2 − Find the addition of two numbers by calling main() and swap() method in Java
Approach 3 − Java program to show the nesting of methods to find out the Perimeter value
Approach 4 − Java inner classes and nested classes
Approach 5 − Java program to show nesting method by using triple method classes
Find the area of a sphere by using nesting method with Java
Let’s find out the area of a sphere by using nesting method with Java. Here we have used two classes, contains with Area() and Volume() which is calculating the area and volume of a sphere.
Example 1
public class Nesting2022 { public void Area(double r){ System.out.println("##### Inside Area method will be applied #####"); double a = 7 * Math.PI * r * r; System.out.println("Surface area of the particular Sphere is : " + a); } public void Volume(double r){ System.out.println("%%%%% Inside Volume method will be applied here%%%%%"); double v = (4 / 3) * Math.PI * r * r * r; System.out.println("Volume of a Sphere is here : " + v); } public static void main(String args[]){ Nesting2022 RDDARB = new Nesting2022(); RDDARB.Area(12); RDDARB.Volume(12); } }
Output
##### Inside Area method will be applied ##### Surface area of the particular Sphere is : 3166.7253948185116 %%%%% Inside Volume method will be applied here%%%%% Volume of a Sphere is here : 5428.672105403162
Addition of two numbers by calling main() and swap() method:
In this Java code, we are trying to show the process of addition of two numbers by calling main() and swap() method.
Example 2
public class Nesting1997 { public void swap(int x, int y){ System.out.println("**@@$$%%This is a swap method. Lets Check The Process%%$$@@**"); System.out.println("Before swapping the condition:x=" + x + " " + "y=" + y); int z = x; x = y; y = z; System.out.println("After Swapping the condition:a=" + x + " "+ "b=" + y); } public void Tutorialspoint16 (int a, int b){ System.out.println("#####This is Tutorialspoint16 encoded method#####"); System.out.println("Before performing the operation we will get:a=" + a + " " + "b=" + b); a = a + 10; b = b + 12; System.out.println("After operation, the system will return:a=" + a + " " + "b=" + b); swap(a, b); } public static void main(String args[]){ Nesting1997 Tutorialspoint07 = new Nesting1997(); int a = 20, b = 30; Tutorialspoint07.Tutorialspoint16(a, b); } }
Output
#####This is Tutorialspoint16 encoded method##### Before performing the operation we will get:a=20 b=30 After operation, the system will return:a=30 b=42 **@@$$%%This is a swap method. Lets Check The Process%%$$@@** Before swapping the condition:x=30 y=42 After Swapping the condition:a=42 b=30
Nesting methods to find out the Perimeter value
In this Java code, we try to show the nesting of methods to find out the Perimeter value.
Example 3
import java.util.Scanner; public class NestingbyCuboid2023{ int perimeter(int l, int b){ int pr = 12 * (l + b); return pr; } int area(int l, int b){ int pr = perimeter(l, b); System.out.println("Perimeter:"+pr); int ar = 6 * l * b; return ar; } int volume(int l, int b, int h){ int ar = area(l, b); System.out.println("Area:"+ar); int vol ; vol = l * b * h; return vol; } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter length of that particular cuboid:"); int l = s.nextInt(); System.out.print("Enter breadth of that particular cuboid:"); int b = s.nextInt(); System.out.print("Enter height of that particular cuboid:"); int h = s.nextInt(); Nesting_Methods obj = new Nesting_Methods(); int vol = obj.volume(l, b, h); System.out.println("Volume:"+vol); } }
Output
Enter length of that particular cuboid:7 Enter breadth of that particular cuboid:16 Enter height of that particular cuboid:10 Perimeter:276 Area:672 Volume:1120
Java inner classes and nested classes
Here in this examples we can demonstrate how to declare some inner classes and nested classes by using a Java environment.
Example 4
//Java code to define an inner class we can use in JVM class CPUz { double price; class Processor{ double cores; String manufacturer; double getCache(){ return 16.10; } } protected class RAM{ double memory; String manufacturer; double getClockSpeed(){ return 07.10; } } } public class Main { public static void main(String[] args) { CPUz cpu = new CPUz(); CPUz.Processor processor = cpu.new Processor(); CPUz.RAM ram = cpu.new RAM(); System.out.println("Processor Cache We Will Get = " + processor.getCache()); System.out.println("Ram Clock speed We Can Examine = " + ram.getClockSpeed()); } }
Output
Processor Cache We Will Get = 16.1 Ram Clock speed We Can Examine = 7.1
Example 4 A
//Java Code To Access Members class Car2022 { String carName; String carType; public Car2022(String name, String type) { this.carName = name; this.carType = type; } private String getCarName() { return this.carName; } class Engine { String engineType; void setEngine() { if(Car2022.this.carType.equals("7XYXARB")){ if(Car2022.this.getCarName().equals("Crysler")) { this.engineType = "Smaller Engine Type"; } else { this.engineType = "Bigger Engine Type"; } }else{ this.engineType = "Bigger Engine Type"; } } String getEngineType(){ return this.engineType; } } } public class Main { public static void main(String[] args) { Car car1 = new Car("Mazda", "16XYZARB"); Car.Engine engine = car1.new Engine(); engine.setEngine(); System.out.println("Engine Type for 16XYZRDD= " + engine.getEngineType()); Car car2 = new Car("Crysler", "7XYZARB"); Car.Engine c2engine = car2.new Engine(); c2engine.setEngine(); System.out.println("Engine Type for 7XYZARB = " + c2engine.getEngineType()); } }
Output
Engine Type for 16XYZRDD= Bigger Engine Type Engine Type for 7XYZARB = Bigger Engine Type
Example 4B
//Java Program To Demonstrate A Static Inner Class Using JVM class MBCSS { static class USB2022{ int usb2 = 2; int usb3 = 1; int getTotalPorts(){ return usb2 + usb3; } } } public class Main { public static void main(String[] args) { MBCSS.USB2022 usb = new MBCSS.USB2022(); System.out.println("Total Ports Present Here In The System = " + usb.getTotalPorts()); } }
Output
Total Ports Present Here In The System = 3
Java program to show nesting method by using triple method classes
In this example, we have written a method specific Java code to show the nesting process by using triple method classes. Here a particular method can call any random methods. It is also possible here to call another method here. That means, method 1 may call method 2, which return may call method 3.
Example 5
public class Nesting0{ public void a1(int a, int b){ a = a + 10; b = b + 20; System.out.println( "******#### Inside the a1 method ####******"); System.out.println("a = " + a + " " + "b = " + b); a2(a, b); } public void a2(int x, int y){ x = x + 100; y = y + 200; System.out.println( "******@@@ Inside the a2 method @@@******"); System.out.println("x = " + x + " " + "y = " + y); } public void a3(int w, int z){ w = w + 50; z = z - 50; System.out.println( "******%% Inside the a3 method %%******"); System.out.println("w = " + w + " " + "z = " + z); a1(w, z); } public static void main(String[] args){ Nesting0 ARBRDD = new Nesting0(); int a = 100, b = 200; ARBRDD.a3(a, b); } }
Output
******%% Inside the a3 method %%****** w = 150 z = 150 ******#### Inside the a1 method ####****** a = 160 b = 170 ******@@@ Inside the a2 method @@@****** x = 260 y = 370
Conclusion
Here, we discussed about the nesting methods with some possible java codes by following the syntaxes and algorithm. Hope this article helps you to understand the modus operandi of the various nesting methods mentioned here.