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 6 of 6

Thread: What is wrong with function return values

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post What is wrong with function return values

    I want to receive the values of the add()
    in the Addition2 Class using the object :

    What is wrong with this code?

    class Addition{
    int a;
    int b;
    int c;

    int add(int a1, int b1, int c1){
    return (a1+b1+c1);
    }
    }
    public class Addition2{
    public static void main(String args[]){
    int temp1, temp2;
    Addition addobj1=new Addition();
    Addition addobj2=new Addition();
    temp1= addobj1.add(200, 20, 20);
    temp2= addobj2.add(100, 10, 10);
    System.out.println(addobj1.temp1);
    System.out.println(addobj2.temp2);
    }
    }


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: What is wrong with function return values

    Explain, please, what addobj1.tem1 and addobj2.temp2 are supposed to evaluate to in your opinion.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with function return values

    I send 3 values to add function, the values are calculated in class Addition, After that, the result is sent back to the main fiunction, so that the values are received in (temp1, temp2)variables. the output is not valid that should be returned value.

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: What is wrong with function return values

    If they are received in temp1 and temp2, then why do you write addobj1.temp1 and addobj2.temp2? "A.b" syntax means "field b in object (class) A".

  5. The Following User Says Thank You to angstrem For This Useful Post:

    Shivafeb17 (June 20th, 2013)

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with function return values

    That worked !!! Thanks :-) but also i had to remove public modifier of class Addition2.. why is that?

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    979
    Thanks
    7
    Thanked 206 Times in 182 Posts

    Default Re: What is wrong with function return values

    Quote Originally Posted by Shivafeb17 View Post
    That worked !!! Thanks :-) but also i had to remove public modifier of class Addition2.. why is that?
    What was the name of the file containing that code, the command you used to compile it, and the message from the compiler?

Similar Threads

  1. issue with return values stuck at 0
    By dendoc01 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2013, 06:00 PM
  2. [SOLVED] Return all values in an ArrayList from a method
    By IanSawyer in forum Collections and Generics
    Replies: 1
    Last Post: March 28th, 2012, 10:53 AM
  3. Need help my values wont return
    By sajeed in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 7th, 2012, 08:46 AM
  4. Need help with my double return function.
    By thisbeme in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 30th, 2011, 10:50 AM
  5. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 04:32 PM