banner



How To Use Variable From Another Method Java

Java Program to pass method call as arguments to another method


In this article, we will understand how to laissez passer method telephone call as arguments to another method. We can call a method from another class by merely creating an object of that class inside another class. After creating an object, call methods using the object reference variable.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter two numbers : 2 and iii

Output

The desired output would exist −

The cube of the sum of ii numbers is: 125

Algorithm

Step 1 - Beginning Footstep 2 - Declare two variables values namely my_input_1 and my_input_2 Footstep 3 - We ascertain a role that takes two numbers, and returns their sum. Step 4 - We define another function that takes one argument and multiplies it thrice, and returns the output. Step five - In the chief office, we create a new object of the course, and create a Scanner object. Step 6 - Now, nosotros can either pre-ascertain the number or prompt the user to enter it. Step 7 - In one case nosotros have the inputs in place, we invoke the function that returns the cube of the input. Step 8 - This result is displayed on the console.

Example ane

Here, the input is being entered by the user based on a prompt. You tin can attempt this example alive in ourcoding ground tool run button.

import java.util.Scanner; public grade Main {    public int my_sum(int a, int b) {       int sum = a + b;       return sum;    }    public void my_cube(int my_input) {       int my_result = my_input * my_input * my_input;       System.out.println(my_result);    }    public static void main(Cord[] args) {       Master obj = new Main();       int my_input_1, my_input_2;       Organization.out.println("Required packages have been imported");       Scanner my_scanner = new Scanner(Organisation.in);       System.out.println("A reader object has been divers ");       System.out.impress("Enter the first number : ");       my_input_1 = my_scanner.nextInt();       Organisation.out.print("Enter the second number : ");       my_input_2 = my_scanner.nextInt();       System.out.println("The cube of the sum of 2 numbers is: ");       obj.my_cube(obj.my_sum(my_input_1, my_input_2));    } }

Output

Required packages have been imported A reader object has been defined Enter the get-go number : 2 Enter the second number : 3 The cube of the sum of two numbers is: 125

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Main {    public int my_sum(int a, int b) {       int sum = a + b;       return sum;    }    public void my_cube(int my_input) {       int my_result = my_input * my_input * my_input;       System.out.println(my_result);    }    public static void master(String[] args) {       Main obj = new Main();       int my_input_1, my_input_2;       my_input_1 = three;       my_input_2 = two;       System.out.println("The 2 number is defined as " +my_input_1 +" and " +my_input_2);       Arrangement.out.println("The cube of the sum of two numbers is: ");       obj.my_cube(obj.my_sum(my_input_1, my_input_2));    } }

Output

The 2 number is defined as 3 and ii The cube of the sum of two numbers is: 125

raja

Updated on 22-February-2022 x:17:42

  • Related Questions & Answers
  • JavaScript phone call() Method with Arguments.
  • Coffee Program to laissez passer lambda expression equally a method argument
  • How to pass arrays every bit part arguments in JavaScript?
  • How to pass a lambda expression every bit a method parameter in Coffee?
  • Java Program to Call One Constructor from another
  • How to phone call an interface method in Java?
  • Java Program to Pass ArrayList as the part statement
  • Pass long parameter to an overloaded method in Java
  • How to pass the individual members as arguments to function using structure elements?
  • How to laissez passer a jQuery consequence every bit a parameter in a method?
  • Call event role from some other method in Controller in SAP
  • How to laissez passer individual members of structure as arguments to part in C language?
  • How to pass arguments to animation.FuncAnimation() in Matplotlib?
  • How to declare, define and telephone call a method in Java?
  • Call append() method to construct a StringBuffer object in Java

How To Use Variable From Another Method Java,

Source: https://www.tutorialspoint.com/java-program-to-pass-method-call-as-arguments-to-another-method

Posted by: coffeysamot1998.blogspot.com

0 Response to "How To Use Variable From Another Method Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel