+ 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 !
5 RĂ©ponses
+ 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
+ 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 !
+ 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
+ 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
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