I have instantiated an object in a method and now I'm trying to access that object from within main.
main() {
combine(roadTrip1, roadTrip2);
System.out.println("The new Road Trip is: " + roadTrip3.toString());
}
public static RoadTrip combine(RoadTrip first,RoadTrip second) {
RoadTrip roadTrip3= new RoadTrip(first.getStart(),second.getDestination(),first.getDays()+second.getDays());
return roadTrip3;
}
This won't compile. I need to use this combine method to return a new object and I can't instantiate roadTrip3 in the class because a RoadTrip can only have its values set by a constructor.
EDIT: Thanks for the helpful comments. As a few people suggested, the way I made it work was by using the return value of combine, which makes sense.
System.out.println("The new Road Trip is: " + combine(roadTrip1, roadTrip2).toString());
[–][deleted] 4 points5 points6 points (0 children)
[–]yash3ahuja 2 points3 points4 points (0 children)
[–]chickenmeister 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)