• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object within an Object - URGENT NEED

 
Plym Guy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I need to create an ArrayList or HasMap in a class which stores objects in it.
For example:
class A{
public String A;
public String B;
ArrayList array = new ArrayList();
public String getStringA(){
return A;}
public void setStringB(String xys){
B= xyz;
return B;
}
public ArrayList getTest(){
return ArrayList;
}
But the problem I have is I need this Array List to hold objects.
In other terms, the ArrayList should be hold an Object containing 3 Strings.
Say Object1:
Its structure should be like:
Obj1{
String 1;
String 2;
Strng 3;
}
Can i create an Object within a class not by instantiating.
I am wondering if to create a class which holds these 3 Strings and then instantiate in this class or create an Inner class within this class.
Which do you think is a better practice.
Hope I am clear with my question. I need a urgent response. Please respond ASAP.
Thanks
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Can i create an Object within a class not by instantiating."
No. That is what the word intantiating means - to create an object (an instance or occurance) of the class.
Now you COULD make the 3 Strings static variables so that they belong to the CLASS and not to a particular instance of the class. The you don't need an object to reference them. It is really hard to say what should be done without knowing what (and WHY) you are trying to accomplish.
Even with inner classes you have to make an instance of the class.
"Which do you think is a better practice."
Depends. WHY are you doing this? Try reading this
Then again I am not sure what you are really trying to ask.
[ April 11, 2003: Message edited by: Cindy Glass ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure why you would want to do this, but you could dynamically create an array of strings wich could be placed in the Collection; you would still have to instantiate the array, but you could do:

...although in this instance you would probably be better served creating a Name class with three attributes: firstName, middleName, lastName.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic