Java Nested Method

March 29, 2022 · Team EasyExamNotes · 3 min read (est.) · 0 comments

Share on WhatsApp
PROGRAM:

To show nested methods.
/**
             * @Prof. Jayesh
             * Here we call a method from main().
             * Methods are in same class so known as nested method.

             */


import java.util.Scanner;


public class ExampleOfNestedMethod {

            static int sum()
            {
                        int a,b,c;
                        System.out.println(“Enter the value of a”);
                        Scanner scn=new Scanner(System.in);
                        a=scn.nextInt();
                        System.out.println(“Enter the value of b”);
                        b=scn.nextInt();
                        c=a+b;
                        return c;
            }

            public static void main(String args[])
            {
                        int d=sum();
                        System.out.println(d);
            }
}

OUTPUT:
Enter the value of a
4
Enter the value of b
6
10


Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion