can someone explain to me how i can pass the array "test" and the int "total" back to the main by reference.


public static void CalculateAverage() throws IOException
{
int[] test=new int[7];
int count;
int total=0;
for (count = 0;count < 5; count++)
{
test[count] = Integer.parseInt(tokenizer.nextToken());
total = test[count] + total;
}
}

can someone explain to me how i can pass the array "test" and the int "total" back to the main by reference.

public static void CalculateAverage() throws IOException
{
int[] test=new int[7];
int count;
int total=0;
for (count = 0;count < 5; count++)
{
test[count] =  Integer.parseInt(tokenizer.nextToken());
total = test[count] + total;
}
}

the " int total" can only be passed by value, not reference, like all primivitive types of java, however, if you really wanted to bypass this you could declare "static int total" as a globabl variable, but this can tend to be bad practice. The test array can be passed through reference, but you might want to change the scope of it and put it outside of the method maybe in main, and then pass the array to the CalculateAverage() method. First off, a tip for java is that your "main" method should probably only consist of one or two lines of code. Let me give you an example of how i would do something like this, I will keep main simple, pass the array by reference, and the int by value.

class Example {

                public static void main (String []args)
                  {
                        Example p = new Example ();
                            p.getStarted;
                            //You could also do "this.getStarted "

                 }

               public void getStarted()
                 {
                       int [] test = new int [7]; 
                       int total = CalculateAverage(total, test);
                 }

               public int CalculateAverage(int total, int test []) throws                IOException
                {



                 for (x = 0;x < 5; xt++)
                     {
                         test[x] = Integer.parseInt(tokenizer.nextToken());
                          total = test[x] + total;
                    }
                    return total;
                   }

I hope this helps.

Java ALWAYS uses pass by value, so your question makes no sense.

Maybe you THINK objects are passed by reference but they aren't. What's passed are references to objects and these are passed by value.

Java ALWAYS uses pass by value, so your question makes no sense.

Maybe you THINK objects are passed by reference but they aren't. What's passed are references to objects and these are passed by value.

Passing a reference of an object thorugh value is passing by reference.
Thats what passing by reference is in java.

Actually, objects are not really passed at all, and the values are the references, yes, but even though there is debate on this topic, the value is a reference, so its being passed by reference.

If it's just calculating the average, do you have to return both of those arrays? I mean, you could just calculate the average and return an int value, instead of returning void. Then you could call the method from main.

>Passing a reference of an object thorugh value is passing by reference.
No, the reference is passed by value, so it's not passing by reference. Google for "java pass reference", it's a common misunderstanding.

>Passing a reference of an object thorugh value is passing by reference.
No, the reference is passed by value, so it's not passing by reference. Google for "java pass reference", it's a common misunderstanding.

Aye, i understand what you, and the 2nd poster, and the google searches are saying.

I understand its not "pass by reference", instead, the reference is passed by value. I understand trust me. However, do you really know what that means?

"
When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. "

Do you see how the pass-by-value changes definition here?

This is due, beacuase the reference is the value, and with that reference you can change the object. Thus, you can logically say, it is pass by reference, since the reference = value.

If you want to get sticky to terminology, you are right, you can not pass by reference, only value, but the OP is not trying to understand this.

I think i can safely say, that when you are talking java, you can say objects are passes by reference, since it invokes differently than with primitive types.

>However, do you really know what that means?
Yes, yes I do.

>When the argument is of primitive type, pass-by-value means that the method cannot change its value.
Incorrect. When the argument is of non-final primitive type, pass-by-value means that the method can change its value all it wants, but the original variable passed (if it was even a variable) will not reflect the changes.

>Do you see how the pass-by-value changes definition here?
A "reference" in Java is closer to a pointer in other languages than a true reference. The reference itself is basically a pointer, and it points to another object. Because a copy of the reference still points to the same object, you can make changes. This still follows the rules of passing by value, and still fails to follow the rules of passing by reference. It's truly unfortunate that references in Java are named as they are because it creates confusion.

>you can say objects are passes by reference, since it invokes differently than with primitive types.
Doing so is anything but safe because the misunderstanding creates a nasty and common pitfall. That's what happens when you try to make things simpler by sacrificing accuracy.

When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. "

I should have cited this, its from java.sun.com. So i doubt this is incorrect

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

Besides that, I agree with everything else you say Narue.

can you delete posts on these forums?

>>However, do you really know what that means?
>Yes, yes I do.
And so do I :)


>>you can say objects are passes by reference, since it invokes differently than with primitive types.
>Doing so is anything but safe because the misunderstanding creates a nasty and common pitfall. That's what happens when you try to make things simpler by sacrificing accuracy.

Correct. If it were pass by reference you could change the actual reference inside the calling method and not just the content of the referenced object.
This code

void method1()
{
  SomeThing a = new SomeThing(1);
  method2(a);
  a.printSomething();
}

void method2(SomeThing s)
{
  s = new SomeThing(2);
}

....

class SomeThing
{
   int something;
   public SomeThing(int s) { something = s; }
   public void printSomething() { System.out.println(something); }
   public void setSomething(int s) { something = s; }
}

would print 2 if Java were pass by reference, when in fact it prints 1.

I should have cited this, its from java.sun.com. So i doubt this is incorrect

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

Besides that, I agree with everything else you say Narue.

And where does that state that Java is pass by reference?
In fact it specifically states that Java uses pass by value exclusively...

>When the argument is of primitive type, pass-by-value means that the method cannot change its value.
Incorrect. When the argument is of non-final primitive type, pass-by-value means that the method can change its value all it wants, but the original variable passed (if it was even a variable) will not reflect the changes.

That was from narue, saying that the first line of this:::

"""When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. """""


was incorrect, and it isnt. This was quoted from Java,sun, nothing more

>was incorrect, and it isnt.
Out of context, it's incorrect. In the calling function, the value will not change, but in the called function, it's just another variable and can be modified. The distinction is subtle, but I think it's important to understand it fully.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.