# Thread: Simple calculation method problem.

1. Member
Join Date
Dec 2010
Posts
18
Rep Power
0

## Simple calculation method problem.

Java Code:
```public class CalculationExample{

//declare global variable
static int y;

public static void main(String[] args){

addition(4, 3); //method called with two values assigned
}//end main

public int addition( int a, int b ){
y=a+b;
return y
}//end addition method
}//end class```
In the above class, the addition method takes an argument of two integer whole numbers
adds both numbers and returns that value as y=7.

My question is, is it possible to work to re-use y equalling to 7 or do I have to recall the entire
method to get the 7 value.

Below is an example of what I mean.

Java Code:
```public class CalculationExample{

//dclare global variable
int y;

public static void main(String[] args){

addition(4, 3);
System.out.print(y); //is it correct that this line prints 7 or does y only obtain a value
when it is beign called in a method form.
}//end main

public int addition( int a, int b ){
y=a+b;
return y
}//end addition method

}//end class```
I'm just trying to figure if after the addition method is run would y obtain the value seven and store it
or if I wanna pull back y as 7 I need to re-enter the entire addition method.

Hope I made myself clear.
Last edited by Eranga; 12-28-2010 at 12:56 AM. Reason: code tags added

2. Your addition() method returns an int (it must be a static method as well, non-static cannot invoke from static). So assign the return value to a int type.

Java Code:
`int y = addition(4, 3);`
Then display it.

Java Code:
`System.out.println(y);`
Last edited by Eranga; 12-28-2010 at 01:24 AM.

3. Senior Member
Join Date
Dec 2010
Posts
100
Rep Power
0
Begin by making your variable static. The answer is yes y will have value 7 or any value you assign to it. Try the following code:

public class CalculationExample{

Java Code:
```	static int y;

public static void main(String[] args){

y = addition(4, 3);
System.out.print(y);
}

public static int addition( int a, int b ){
return a + b;
}```

4. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
10
if you declare the addition function without static, then you need to instantiate a CalculationExample object in order to use it
Java Code:
```public class CalculationExample{
public static void main(String[] args){
int y;
CalculationExample add = new CalculationExample();
y = add.addition(4, 3);
System.out.print(y); //is it correct that this line prints 7 or does y only obtain a value
//                          when it is beign called in a method form.
}//end main

public int addition( int a, int b ){
return a+b;
}//end addition method

}//end class```
otherwise, change your addition method to static
Java Code:
```    public static int addition( int a, int b ){
return a+b;
}//end addition method```
also, declare a global variable when you really need to, otherwise, use local variables.

5. Originally Posted by user0
Begin by making your variable static. The answer is yes y will have value 7 or any value you assign to it. Try the following code:

public class CalculationExample{

Java Code:
```	static int y;

public static void main(String[] args){

y = addition(4, 3);
System.out.print(y);
}

public static int addition( int a, int b ){
return a + b;
}```
Really no need to define as static. Avoid global variables as much as possible.

Within the scope the value never changed.

6. Member
Join Date
Dec 2010
Posts
18
Rep Power
0
I appreciate that help, I knew I forgot something turns out you all were right static wasn't stated so the variable didn't keep the value after calculation.

Thanks again.

BTW, what exactly do you mean to put the code in tags...

7. Member
Join Date
Dec 2010
Posts
18
Rep Power
0
Hey I thought that would have worked, however after work I went and tried declaring it both;
static int y;
and
public static int y;

neither of which worked.

Is there something else I should have done?

It is storing a value but not the accurate for some reason it is giving me a completely different figure.

8. Member
Join Date
Dec 2010
Posts
18
Rep Power
0
oh btw, I'm sorry I havn't been using tags but i'm a beginner to java and we havn't touched HTML as yet.

Hope that isn't too much of a problem..

9. Originally Posted by carman12
oh btw, I'm sorry I havn't been using tags but i'm a beginner to java and we havn't touched HTML as yet.

Hope that isn't too much of a problem..
Of course not, and that's not HTML. Read it carefully and give a try in next post.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•