File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Object within an Object - URGENT NEED Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object within an Object - URGENT NEED" Watch "Object within an Object - URGENT NEED" New topic

Object within an Object - URGENT NEED

Plym Guy

Joined: Apr 10, 2003
Posts: 3
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:
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.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
"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 ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Object within an Object - URGENT NEED
It's not a secret anymore!