I'm trying to make a method where I can view the object. How can I access the bag1 object from the view() method?
public class ArrayBagUtilities {
public void Create() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Size of bag 1: ");
int size = keyboard.nextInt();
Bag bag1 = new ArrayBag(size);
keyboard.nextLine();
String itemStr;
for (int i = 0; i < size; i++) {
int n = i+1;
System.out.print("Item " + n + ": ");
itemStr = keyboard.nextLine();
bag1.add(itemStr);
}
}
public void view() {
System.out.print(bag1);
}
}
Bag bag1;outside ofCreate(): make it a class member, instead of a "local variable".public class ArrayBagUtilities { private Bag bag1; public void Create() {...} ...}