Simple calculation method problem.

• 12-28-2010, 01:41 AM
carman12
Simple calculation method problem.
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 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.

Code:

public class CalculationExample{

//dclare global variable
int y;

public static void main(String[] args){

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 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.

• 12-28-2010, 01:56 AM
Eranga
Please use code tags next time.
• 12-28-2010, 01:58 AM
Eranga
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.

Code:

Then display it.

Code:

System.out.println(y);
• 12-28-2010, 02:12 AM
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{

Code:

static int y;

public static void main(String[] args){

System.out.print(y);
}

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

• 12-28-2010, 02:15 AM
JavaHater
if you declare the addition function without static, then you need to instantiate a CalculationExample object in order to use it
Code:

public class CalculationExample{
public static void main(String[] args){
int y;
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 class

Code:

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

also, declare a global variable when you really need to, otherwise, use local variables.
• 12-28-2010, 02:20 AM
Eranga
Quote:

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{

Code:

static int y;

public static void main(String[] args){

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.
• 12-28-2010, 02:44 AM
carman12
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...
• 12-29-2010, 02:39 AM
Eranga
Quote:

Originally Posted by carman12
BTW, what exactly do you mean to put the code in tags...

Java Forums - BB Code List
• 12-29-2010, 02:51 AM
carman12
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.
• 12-29-2010, 02:52 AM
Eranga
Have you done what I told?

• 12-29-2010, 02:53 AM
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..
• 12-29-2010, 02:56 AM
Eranga
Quote:

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.