0
import java.util.ArrayList;
public class Watch{
// instance variables for the class watch
private String name; 
private long serial_no;
private String desc;
private String color;
private double price;
private double weight;
private int hour;
private int min;
private int sec;
ArrayList<Watch> watchStore = new ArrayList<Watch>();
public void addWatch(String nme, long s_r, String ds, String cl, double pr, double wg){
 watchStore.add(new Watch(nme, s_r, ds, cl, pr, wg));
}
public String disWatch(){
 return watchStore.get(watchStore) + "";
}

In my addWatch method I am creating a new Watch to add to an ArrayList, inside of the Watch class. Is this possible?

Zarwan
5,8774 gold badges32 silver badges48 bronze badges
asked Oct 11, 2015 at 1:48
0

2 Answers 2

2

I guess it would be better to have two classes: Store and Watch, where the class Store contains a list like ArrayList<Watch> watches;.

Class Store:

public class Store {
 private ArrayList<Watch> watches;
 public Store() {
 watches = new ArrayList<>();
 // Add first watch
 watches.add(new Watch(...));
 }
}

And the class Watch:

public class Watch {
 private String name; 
 private long serial_no;
 private String desc;
 private String color;
 private double price;
 private double weight;
 private int hour;
 private int min;
 private int sec;
 public Watch(...) {
 // ...
 }
}
answered Oct 11, 2015 at 1:57
Sign up to request clarification or add additional context in comments.

1 Comment

Let me know, if you have any trouble. I will then update my answer.
0

Yes, why not? But make more a container class to store watches (WatchStore).

answered Oct 11, 2015 at 1:51

2 Comments

ok but how would that be...??? because I tried what I did in the addWatch method with the disWatch method but no success...
Why do you want to store watches inside a watch class, make a WatchStore class and store watches inside that class.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.