The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

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

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:

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

Into this 1:

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:

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.

Re: Simple Static Method Problem

The code was removed for the simple fact that it was a "here, your problem is now fixed" reply. In many cases this denies one the chance to problem solve - one of the most important and hardest to learn skills of programming - but also violates academic policies at many learning institutions.