In the Value & Reference Types course | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In the Value & Reference Types course

In the Value & Reference Types course (1/2) , is addOneTo(x) a FUNCTION or a VARIABLE DECLARATION ? public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); // => IS THAT a FUNCTION or a VARIABLE DECLARATION ? System.out.println(x); } static void addOneTo(int num) { num = num + 1;}} // Outputs "5" Thanks !

11th Nov 2018, 6:48 AM
S. Kaouche
S. Kaouche - avatar
5 Answers
+ 10
public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); //method call System.out.println(x); } static void addOneTo(int num) { //method definition num = num + 1;}} //outputs 5 as its .... addOneTo(x); .... NOT .. x=addOneTo(x); . . . //have a look at my answer here also , its different , your question is fine☺👍 https://www.sololearn.com/Discuss/1570378/?ref=app
11th Nov 2018, 7:06 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
thanks a lot Yehoraz Levi and Gaurav Agrawal ! I m new beginner I didn't know that you can put a method in a variable declaration . That's impressing ! Thanks a lot for your help !
11th Nov 2018, 8:46 PM
S. Kaouche
S. Kaouche - avatar
+ 1
you’r welcome just notice that it is not a method in a variable declaration (though you are able to do it), the variable declaration is the line “int x = 5;” but after you declar the variable you can use the method “on” it to manipulate the variable the way you want, lets take example here: public class Program { public static void main(String[] args) { int NewNum = 10; HugeNum(NewNum); int NewOther = 5; Other(NewOther); System.out.println(NewNum + " " + NewOther); System.out.println(HugeNum(NewNum) + " " + NewOther); System.out.println(NewNum + " " + HugeNum(NewNum) + " " + HugeNum(NewOther) + " " + NewOther); } static int HugeNum(int x) { x *= 30000; return x; } static void Other(int x) { x += x; } } if its hard to read here tell me i will send a link to the code itself, as you can see the program prints both “NewNum” and “NewOther” once on “normal status” and twice when i used the method i create to manipulate them, you can see how the methods affect the variables
12th Nov 2018, 2:03 AM
Yehoraz Levi
Yehoraz Levi - avatar
+ 1
here you can see the code and change it the way you want try diffrent things so youll understand better https://code.sololearn.com/c1VBeN4T03pe/?ref=app
12th Nov 2018, 2:04 AM
Yehoraz Levi
Yehoraz Levi - avatar
0
its a method you create, for example you can create a method called add5 that adds 5 to the number like this: int addfive (int a) { a += 5; return a; } for the object itself or do this: static void addfive (int b) { b+=5; } and call the method “on the spot” like in your post for the variable
11th Nov 2018, 7:40 AM
Yehoraz Levi
Yehoraz Levi - avatar