0

I am new to java. I have created the following two classes for adding cities (partial code has been shown here):

public class TourManager {

// Holds our cities
private static ArrayList destinationCities = new ArrayList<City>();

// Adds a destination city
public static void addCity(City city) {
    destinationCities.add(city);
}
}

public class Tour{

// Holds our tour of cities
private ArrayList tour = new ArrayList<City>();

// Constructs a blank tour
public Tour(){
    for (int i = 0; i < TourManager.numberOfCities(); i++) {
        tour.add(null);
    }
}
 public ArrayList getTour(){
    return tour;
}

}

Now I have created the following two tour class objects:

Tour currentSolution = new Tour();
Tour newSolution = new Tour(currentSolution.getTour());

I have declared two functions as follows:

void Fuction1(Tour newSolution);
void Fuction2(Tour newSolution);

I want to pass the newsolution object as an argument to the above two functions so that we can manipulate the original object inside both of these functions So, truly speaking, I want to pass the value by reference. Now If I pass it in the following way:

Function1(newsolution);
Function2(newsolution);

Will it change the original object inside both of these functions? If not, then what is the proper way of doing this?

  • if you need to mutate passed object inside a method, you can easily do it unless class is immutable. To express this intention, consider using `final` for arguments, like: `void function1(final Tour tour)` – Alex Salauyou Apr 22 '16 at 13:17
  • If I write like this: for declaration: void Fuction1(final Tour newSolution); and for calling: Function1(newsolution); will it change the value of the original object if i manipulate it inside the function? @Sasha Salauyou –  Apr 22 '16 at 13:20
  • If you don't reassign `newSolution` inside the method and make manipulations on it, it will be changed and those changes will be visible outside, so yes. – Alex Salauyou Apr 22 '16 at 13:22
  • @user6216509 yes it will, as in Java you are passing objects by reference. there is no other way. – Andy Apr 22 '16 at 13:22
  • @SashaSalauyou you are wrong! 1) `final` only specifies, that you can not assing a different value to the variable; if the object has mutators, the callee can use them to manipulate the object. 2) reassigning in Java is also by reference -> see 1) – Andy Apr 22 '16 at 13:24
  • @Andy Did I say that `final` means "immutable object"? No. And you're completely wrong about reassigning reference. If you re-reference *passed object* inside a method, this won't be visible outside -- because *reference* itself is passed *by value*. – Alex Salauyou Apr 22 '16 at 13:26
  • so, what I have shown in the above code is right. I mean for declaration: void Fuction1( Tour newSolution); and for calling: Function1(newsolution); . Then if I change the object inside function1, it will also change the original object. right? @Andy –  Apr 22 '16 at 13:26
  • @Andy read carefully: *To express this intention, consider using final for arguments*. -- to make sure you work with passed object and don't ocassionally reassign it locally. – Alex Salauyou Apr 22 '16 at 13:27

0 Answers0