Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default How do you assign the result of a method call to a variable?

    I was wondering if it is possible to call a method that returns a value, then assign it to a variable.

    Java Code:
    private int raise;
    
    raise = paycheck.raise();

    The variable 'raise' is of type int. Then we call a method called 'raise' which is a method in the object called 'paycheck.' Suppose that raise() multiplies a salary by a percentage, then returns that calculated result.

    If this is possible, how would you invoke the method call?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do you assign the result of a method call to a variable?

    Quote Originally Posted by allDogsAreGood View Post
    I was wondering if it is possible to call a method that returns a value, then assign it to a variable.
    Of course. Any method which has a return type other than void must return a value. But it is not required to be assigned to a variable.


    If this is possible, how would you invoke the method call?
    Just like you did above. However, it can be confusing to have a method and variable the same name. So you may want to change that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 07-04-2014, 09:37 AM
  2. Assign request variable in catch block
    By anjibman in forum New To Java
    Replies: 3
    Last Post: 11-06-2012, 02:13 AM
  3. Assign a value to a final variable
    By yma16 in forum New To Java
    Replies: 6
    Last Post: 05-08-2011, 05:27 AM
  4. call servlet result page in frameset
    By reubenmk in forum Java Servlet
    Replies: 0
    Last Post: 08-05-2010, 10:44 AM
  5. Assign it, or call it again,, (again!)
    By sonny in forum New To Java
    Replies: 4
    Last Post: 03-17-2010, 03:47 AM

Tags for this Thread

Posting Permissions

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