I know that if you pass a Class type as a parameter for a method the pointer will be passed and if you edit the parameter in the method you also edit the original object instance you passed (for example if a method named public void changeColor(Color color)
does the following code:
public void changeColor(Color color)
{
color = new Color(20, 100, 50);
}
and you called the value like so:
Color color = new Color(10, 20, 30);
System.out.println(color.toString());
changeColor(color);
System.out.println(color.toString());
both lines of the console should be different (java.awt.Color[r=10,g=20,b=30]
and java.awt.Color[r=20,g=100,b=50]
), thus the value is changed and the pointer is passed. On the other hand, if you use an int value, which is a primitive, you get different result:
public void changeNumber(int i)
{
i = 10;
}
and you called the value like so:
int n = 5;
System.out.println(n);
changeNumber(n);
System.out.println(n);
The console does not print 5
and 10
but on both lines says 5
. With that said, if I had this:
public class A
{
public int i;
public Color c;
public A(int n, Color color)
{
i = n;
c = color;
}
}
And in the runnable class...
public static void main(String[] args)
{
A a = new A(10, new Color(10, 40, 23));
changeA(a);
}
public static changeA(A varA)
{
varA.i = 54;
varA.c = new Color(40, 30, 264)
}
Will i inside the A class also act as if it were part of the pointer of A or will the value if a.i in the main method not change after I run changeA(a); If not, what can I do so it does change?