Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can I do this with methods in Java?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can I do this with methods in Java?

    I need to run a method 4 times and store the data gained somewhere so I can display it later.
    is this statement correct? if not is there any way to do this?
    public void myMethod()
    {
    //stuff in the method
    }

    callOne = myMethod();
    callTwo = myMethod();
    callThree = myMethod();
    callFour = myMethod();


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Can I do this with methods in Java?

    If you want a method to return a value, you have to declare it as so.

    public void myMethod()

    This method can not return a value, because you have defined its return type as void. For example, if you want a method to return an int, you must do this:

    public int myMethod()

    You can also have methods return objects:

    public String myMethod()

    Then, all you have to do is a) in the method body, make sure a variable or value of the correct type is returned and b) make sure that the variable to which you are assigning the method's value is correct.

    For example, if you have this method:
    public double myMethod()
    {
       return Math.random()*100; //return type is double, this returns a double
    }

    You could assign the method's value to a variable, but only if that variable is a double...

    double myDouble = myMethod();
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Can I do this with methods in Java?

    Instead of having 4 different variables, you could use an array with 4 slots.

Similar Threads

  1. Help with assignments Methods Java
    By jocke01 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2011, 08:38 PM
  2. Using Protected Methods, in a Java Library
    By WACman in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 04:42 PM
  3. 2 Java methods and a sorting algorithm
    By Tiberius in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 26th, 2011, 02:41 PM
  4. Methods in java class files are not recognized.
    By Girish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2010, 05:44 AM
  5. Novice with java methods, please help!
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2009, 04:23 AM