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.


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

Results 1 to 4 of 4

Thread: Trying to return more than one value as object

  1. #1
    Join Date
    Aug 2011
    Thanked 3 Times in 3 Posts

    Default Trying to return more than one value as object

    I am pretty new to Java. I tried to look at a few examples and they made no sense to me. Here is what I am trying to do:
    Create a method that just handles the calculations for the application. Create another method that allows returning all the values from the
    method used to do the calculations. Have the main method only hold the initialized variables and print out the results. Here is what I tried
    (pure conjecture and I cannot figure out how to accomplish what I want):

    public class WorkingWithMults
    	public static void main(String[] args)
    		int a = 1, b = 2;
    	public static void Calculations(int a, int b)
    		a = a + 5;
    		b = b + 5;
    	public Calculations returnValue()
    		Calculations obj = new Calculations();
    		return obj;

    Thanks in advance!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,635 Times in 2,605 Posts

    Default Re: Trying to return more than one value as object

    If the returnValue method is to return the values computed by the calculate (not Calculations) method, it needs to include those in the object it returns. Your Calculations constructor should take those values as arguments.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    My Mood
    Thanked 82 Times in 78 Posts
    Blog Entries

    Default Re: Trying to return more than one value as object

    @mwr76: You must start learning from basics. It looks like as you directly started reading classes and that stuff.

  4. #4

    Default Re: Trying to return more than one value as object

    This is dangerous and not very OOPish but you can create an array of Objects and return that. The only danger will be not knowing what comes out on the other side. You will have to cast the data to use it.

    Create a class Calculation that performs the calculation and gets returned.

    public class Calculation
         private double binaryOperand1;
         private double binaryOperand2;
         private double result;
         public Calculation(double op1, double op2)
              this.binaryOperand1 = op1;
              this.binaryOperand2 = op2;
              this.result = null;
         public Calculation()
         //Getters and Setters elided...
         public Calculation addOperation()
              result = binaryOperand1 + binaryOperand2;
              return this;
         //... and so on 
    Kenneth Walter
    Software Developer

Similar Threads

  1. Calling from one javabean object to other javabean object in a single class
    By kichkich in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2011, 07:20 AM
  2. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. how to return value ..
    By gr8mind in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 11th, 2011, 04:27 AM
  5. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM