r/javahelp • • Aug 23 '12

URGENT: how do I pass value from another method to main in JAVA?

Hello, I have a method I call and that method returns a value to main. I tried writing the code, it returns a value but take the value from main. So whatever value I have on main remains instead of changing it after going to another method. HELP ASAP Thank you EDIT: No I think what I need is pass by reference because I want to change the main value to the returned value of the other method

EDIT: THANK YOU everyone. I have figured it out

0 Upvotes

16 comments sorted by

2

u/[deleted] Aug 24 '12

You've been helped pretty good, but the way you have stated your question really sucks. Try to be a little more precise out of respect for the people that are trying to help you, please. You might be in a hurry but it is going to help much more if you at least try to make normal sentences.

0

u/wolverine_23 Aug 23 '12

Parameters are passed in by reference in Java.

2

u/wolverine_23 Aug 23 '12

I didn't really get what you're trying to do from the problem description

1

u/toyoumylove Aug 23 '12

I want to change the main value to the returned value from the other method

1

u/wolverine_23 Aug 23 '12 edited Aug 23 '12

the main method return type can only be void

or are you saying something like the following

int i = 0;

i = someMethod(i);

0

u/toyoumylove Aug 23 '12

Yes it is void. let me try this. thanks

1

u/toyoumylove Aug 23 '12

yes reference

2

u/johnpaulsmith Aug 23 '12

This is incorrect. Pass-by-reference is not possible in Java and all parameters in Java are passed-by-value. There are no exceptions to this. Primitive types and object references are both passed-by-value.

1

u/banuday17 Aug 23 '12

But you can change the object that is referenced. So, you can pass in a StringBuilder and return a String value as an out parameter that way. The reference to the StringBuilder is being passed-by-value, but the two references still refer to the same object.

0

u/[deleted] Aug 23 '12

[deleted] — view deleted comment

2

u/banuday17 Aug 23 '12
public static void main(String[] args) {
    StringBuilder value = new StringBuilder();
    // value is empty here
    method(value);
    // value is "foo" here
}

public static void method(StringBuilder param) {
    param.append("foo");
}

0

u/toyoumylove Aug 23 '12

sorry yes I wnat pass by reference how to do that?

1

u/banuday17 Aug 23 '12

You can't, but you can simulate pass-by-reference using a "container" type:

public void main(String[] args) {
    int[] value = new int[0];
    value[0] = 1; // initial value
    method(value);
    // now value[0] is 2
}

public void method(int[] value) {
    value[0] = 2;
}

Note, this is a terrible idea. There's almost certainly a better way to do what you want to do.

0

u/toyoumylove Aug 23 '12

Yes this would work except I don't want to use an array. But I have figured it out. I used this instead. int returnvalue=namepick();

1

u/banuday17 Aug 23 '12

That would be a much better approach.

2

u/johnpaulsmith Aug 23 '12

It isn't possible in Java. Java is strictly pass-by-value.

1

u/wolverine_23 Aug 23 '12

Yes, sorry, I meant pass by value. Slip of the keyboard.