Depends. If your object has state (represents a row in a database for example), you would have to create it for each iteration. But if all you need to do is call a method in the object (i.e. if it is stateless, (has no non-static and non-final class variables or is immutable)), you should just create one instance, and call the method in each iteration. An example of that is your Scanner object. All you are doing is call a method in it, so instead of creating a new object each time, you would either create it before calling the method, or as an instance (class level, ideally private) field so it can be re-used in all methods. Here is how I would re-write your code:
public class MyClass {
private final Scanner scanner = new Scanner(System.in);
public void doSomething() {
Object object = new Object();
do {
// call your method here
// object.yourMethod();
System.out.println("Do you wish to continue entering data?");
} while (scanner.nextLine().equalsIgnoreCase("Yes"));
}
}
OTHO if you want to store the state associated with each instance you create, then you would do it like this:
public class MyClass {
private final Scanner scanner = new Scanner(System.in);
public void doSomething() {
List<Object> data = new ArrayList<Object>();
Object object;
do {
// call your method here
object = new Object();
// object.yourMethod();
data.add(object);
System.out.println("Do you wish to continue entering data?");
} while (scanner.nextLine().equalsIgnoreCase("Yes"));
for(Object d : data) {
// do something with the info you captured
}
}
}