# Simple Static Method Problem

• August 1st, 2013, 03:30 PM
nortski
Simple Static Method Problem
Good evening all, I am a new member to this forum, it seems like a great place for beginners like me!

I am at the very beginning of my Java education and I am having a little trouble with an exercise involving Static Methods.

This exercise is designed to introduce me to invoking static methods inside a class without instantiating an object. Basically; I am just trying to print out the value of a returned calculation, but I am getting a compile error. I'll paste me code below. Thanks in advance for any guidance :)

public class MyMath {

public static long square(int a){

long b = (long) (a*a);
return b;
}
}

public class Squares {

public static void main(String[] args) {

MyMath.square(12);
System.out.println(MyMath.square());

}
}
• August 1st, 2013, 03:36 PM
llowe29
Re: Simple Static Method Problem
...
correct code
• August 1st, 2013, 03:40 PM
copeg
Re: Simple Static Method Problem
• August 1st, 2013, 03:43 PM
llowe29
Re: Simple Static Method Problem
But it was two little changes which anyone could have said in writing and the poster already had a full program. How is that spoonfeeding?
• August 1st, 2013, 03:46 PM
GregBrannon
Re: Simple Static Method Problem
I hope you see what llowe29 did was to combine the best parts of 2 lines of your program into one:

These 2:
Code :

```MyMath.square(12); System.out.println(MyMath.square());```

Into this 1:
Code :

`System.out.println(MyMath.square(12));`

In the first line of your code, MyMath.square(12); gets the right answer but then discards it, because it isn't stored anywhere. Instead, storing the variable and using it in the print statement would also work:
Code :

```long result = MyMath.square(12); System.out.println( result );```
But if the variable result is not needed anywhere else, llowe29's version would be preferred.

Similarly, the method square() could be shortened to:
Code :

``` public static long square(int a) { return a * a; }```
• August 1st, 2013, 03:47 PM
nortski
Re: Simple Static Method Problem
I take it I've just been denied the help???? Thanks llowe29 for the help, I didn't get a chance to see it.

For the moderators information, I have tried many variations of the code and Google'd the problem, but as yet was unable to find the help.

At what point is it no longer spoon feeding?
• August 1st, 2013, 03:48 PM
llowe29
Re: Simple Static Method Problem
Thamks Greg for demonstrating my point and adding on.
• August 1st, 2013, 03:56 PM
GregBrannon
Re: Simple Static Method Problem
Quote:

Originally Posted by nortski
I take it I've just been denied the help???? Thanks llowe29 for the help, I didn't get a chance to see it.

For the moderators information, I have tried many variations of the code and Google'd the problem, but as yet was unable to find the help.

At what point is it no longer spoon feeding?

• August 1st, 2013, 04:00 PM
nortski
Re: Simple Static Method Problem