Results 1 to 5 of 5
  1. #1
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default 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,

    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);
    	}
    	
    }
    But I only can every get a memory location back from 'Methods', I cannot get a usable answer.
    What am I doing so wrong?

    This is the Output:
    Methods driving me mad-screenshot003.jpg
    Last edited by GrumpyBum; 04-22-2012 at 05:31 AM. Reason: Added Output

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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:

    Java Code:
    someMethod(2);
    and are essentially tossing out any result returned by this method.

    Instead, you should be doing this:

    Java Code:
    int someVariable = someMethod(2);
    Then you can use the result that is held by the variable.

  3. #3
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

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

    Java Code:
     Methods anotherNumber = new Methods();
     int test = anotherNumber.Methods(2);
     System.out.println(test);
    Time to take a break I think.

    Thank you for your quick response.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  5. #5
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Methods driving me mad

    Before I was getting Methods@12821f I was only getting compiler errors, this is actually my first time working with Methods and Constructors. I got the constructors first time so I was happy about that.

Similar Threads

  1. Image sizing is driving me nuts!!!
    By knightwriter in forum New To Java
    Replies: 3
    Last Post: 12-30-2011, 06:02 PM
  2. Driving me crazy!!!! Please help!!
    By Quizzle23 in forum New To Java
    Replies: 33
    Last Post: 02-24-2011, 09:00 AM
  3. image does not refresh-driving me crazy
    By jambon in forum AWT / Swing
    Replies: 1
    Last Post: 04-09-2010, 05:25 PM
  4. THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
    By syntrax in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 05:27 AM
  5. Loop driving me loopy!!!!!
    By soc86 in forum New To Java
    Replies: 8
    Last Post: 01-16-2009, 02:00 AM

Posting Permissions

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