Results 1 to 5 of 5
Thread: Methods driving me mad
- 04-22-2012, 04:29 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Methods driving me mad
Hi All,
I have been working all morning to make sense of methods but without defining a method as static I am getting really confused.
I have been testing with the following,
But I only can every get a memory location back from 'Methods', I cannot get a usable answer.Java Code:public class Methods { public static int staticMethod(int input) { input++; return input; } public int Methods(int input) { input++; System.out.println("HelloWorld"); return input; } public static void main(String[] args) { int runNumber = 3; runNumber = staticMethod(runNumber); System.out.println(runNumber); Methods anotherNumber = new Methods(); anotherNumber.Methods(2); System.out.println(anotherNumber); } }
What am I doing so wrong?
This is the Output:
Last edited by GrumpyBum; 04-22-2012 at 04:31 AM. Reason: Added Output
-
Re: Methods driving me mad
When you call your non-void method, you need to assign what is returned to a variable. You're doing this:
and are essentially tossing out any result returned by this method.Java Code:someMethod(2);
Instead, you should be doing this:
Then you can use the result that is held by the variable.Java Code:int someVariable = someMethod(2);
- 04-22-2012, 04:51 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Re: Methods driving me mad
That does make perfect sense, I thought I tried that but I have just tried again and it has worked.
I thinks that I have been trying to work this out for long enough that I am making mistakes now.
Time to take a break I think.Java Code:Methods anotherNumber = new Methods(); int test = anotherNumber.Methods(2); System.out.println(test);
Thank you for your quick response.
-
Re: Methods driving me mad
You're quite welcome.
As an aside, what you were seeing printed before, Methods@19821f, was the String returned by Method's public toString() method. Since you didn't give Method this method (now that's confusing to say!), it uses the default toString() method of its parent class, Object, and this will return the class name, the '@' character and the object's hashCode. If you ever want to have an object's toString() method make sense, if you want to call System.out.println(myObject) and have it print out something that his helpful, you will want to give your class its own toString() method that returns a String that usually prints out one or more of the values held by the fields of the object.
- 04-22-2012, 05:09 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Image sizing is driving me nuts!!!
By knightwriter in forum New To JavaReplies: 3Last Post: 12-30-2011, 05:02 PM -
Driving me crazy!!!! Please help!!
By Quizzle23 in forum New To JavaReplies: 33Last Post: 02-24-2011, 08:00 AM -
image does not refresh-driving me crazy
By jambon in forum AWT / SwingReplies: 1Last Post: 04-09-2010, 04:25 PM -
THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
By syntrax in forum New To JavaReplies: 2Last Post: 12-18-2009, 04:27 AM -
Loop driving me loopy!!!!!
By soc86 in forum New To JavaReplies: 8Last Post: 01-16-2009, 01:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks