Java code, passing parameter to method


i sing

I am not sure what I am doing wrong here, here is my code

package methods;

public class example {
    public static int sum(int x, int y){ 
        return x+y; 
    }
    public static void printSomething() {
        int a = 1; 
        int b = 2; 
        System.out.println("The sum of "+ x + " and "+ y +" is "+sum(a,b)); 
    }
    public static void main(String[] args) {
        System.out.println("Hello"); 
        printSomething();
    }
}

I want to print the sum of x and y is 3

Oscar Lopez

Try this:

System.out.println("The sum of "+ a + " and "+ b +" is "+sum(a,b)); 

The parameter name xsum yis local to the method definition and is called sum in the current scope .ab

Alternatively, and for coherence reasons, you can simply rename aand buse the xand method. The result will be exactly the same, but now the variable will have the same name.yprintSomething()

Related


JAVA - Passing generic enum as method parameter of constructor

Juan Carlos Zarate: I have three different types of enums (itemsA, itemsB and itemsC) and a "Product" class. So far, I'm passing an enum as a parameter to my product constructor with three constructors: static Product actualProduct; public static void

Java generic method parameter passing problem

user1085876: I have a question about passing parameters to generic methods. code show as below: public class View<T extends View<T,PM>, PM extends Source> { protected PM source; protected EventManager<T, PM> eventManager; public View(PM s){

Java equivalent of passing object as method parameter in GoLang

Alan: Assuming I'm bored System.out.println(message)everywhere , I've introduced in Java a method that looks like private void print (Object message) { System.out.println(message); } I will call print (2)& in java when necessary .print ("hi") Can GoLang ac

JAVA - Passing generic enum as method parameter of constructor

Juan Carlos Zarate: I have three different types of enums (itemsA, itemsB and itemsC) and a "Product" class. So far, I'm passing an enum as a parameter to my product constructor with three constructors: static Product actualProduct; public static void

Java - passing method by parameter

amateurs I'm trying to create a method that allows me to utilize what I think is a lambda to execute a method over a series of connections. After some research, this is the code I came up with, but it doesn't work: performGlobalAction(()->{ // doSomething(

Ruby passing method(?) as parameter?

username In Ruby, I have a bunch of methods that return information from a database: a list of states, countries, languages, races, etc, options for an application form. I know the id selected from the form and am writing a method to get the name from the id:

Passing a method as a parameter to a Java method

Jonathan I have some Java code that I want to convert to Xtend. The Java code is: public void createPartControl(Composite parent) { final Canvas clock = new Canvas(parent, SWT.None); clock.addPaintListener(this::drawClock); } private void drawClock(Pa

Java code, passing parameter to method

i sing I am not sure what I am doing wrong here, here is my code package methods; public class example { public static int sum(int x, int y){ return x+y; } public static void printSomething() { int a = 1; int b = 2;

Java equivalent of passing object as method parameter in GoLang

Alan: Assuming I'm bored System.out.println(message)everywhere , I've introduced in Java a method that looks like private void print (Object message) { System.out.println(message); } I will call print (2)& in java when necessary .print ("hi") Can GoLang ac

Java generic method parameter passing problem

user1085876: I have a question about passing parameters to generic methods. code show as below: public class View<T extends View<T,PM>, PM extends Source> { protected PM source; protected EventManager<T, PM> eventManager; public View(PM s){

Passing a method as a parameter to a Java method

Jonathan I have some Java code that I want to convert to Xtend. The Java code is: public void createPartControl(Composite parent) { final Canvas clock = new Canvas(parent, SWT.None); clock.addPaintListener(this::drawClock); } private void drawClock(Pa

Java passing method reference as parameter of other method

WE Coyote I'm trying to pass the selected class A "get" method to a method in class B. I've checked out Java Pass Method as Parameter , but I can't get the interface method to work in a reasonable way. I'd rather not use Java 8 (lambdas) and avoid reflection a

Java - passing method by parameter

amateurs I'm trying to create a method that allows me to utilize what I think is a lambda to execute a method over a series of connections. After some research, this is the code I came up with, but it doesn't work: performGlobalAction(()->{ // doSomething(

Ruby passing method(?) as parameter?

username In Ruby, I have a bunch of methods that return information from a database: a list of states, countries, languages, races, etc, options for an application form. I know the id selected from the form and am writing a method to get the name from the id:

Java - passing method by parameter

amateurs I'm trying to create a method that allows me to utilize what I think is a lambda to execute a method over a series of connections. After some research this is the code I came up with, but it doesn't work: performGlobalAction(()->{ // doSomething()

Java code, passing parameter to method

i sing I am not sure what I am doing wrong here, here is my code package methods; public class example { public static int sum(int x, int y){ return x+y; } public static void printSomething() { int a = 1; int b = 2;

JAVA - Passing generic enum as method parameter of constructor

Juan Carlos Zarate: I have three different types of enums (itemsA, itemsB and itemsC) and a "Product" class. So far, I'm passing an enum as a parameter to my product constructor with three constructors: static Product actualProduct; public static void

Java generic method parameter passing problem

user1085876: I have a question about passing parameters to generic methods. code show as below: public class View<T extends View<T,PM>, PM extends Source> { protected PM source; protected EventManager<T, PM> eventManager; public View(PM s){

Java equivalent of passing object as method parameter in GoLang

Alan: Assuming I'm bored System.out.println(message)everywhere , I've introduced in Java a method that looks like private void print (Object message) { System.out.println(message); } I will call print (2)& in java when necessary .print ("hi") Can GoLang ac

Passing a method as a parameter to a Java method

Jonathan I have some Java code that I want to convert to Xtend. The Java code is: public void createPartControl(Composite parent) { final Canvas clock = new Canvas(parent, SWT.None); clock.addPaintListener(this::drawClock); } private void drawClock(Pa

Java - passing method by parameter

amateurs I'm trying to create a method that allows me to utilize what I think is a lambda to execute a method over a series of connections. After some research this is the code I came up with, but it doesn't work: performGlobalAction(()->{ // doSomething()

Java code, passing parameter to method

i sing I am not sure what I am doing wrong here, here is my code package methods; public class example { public static int sum(int x, int y){ return x+y; } public static void printSomething() { int a = 1; int b = 2;