Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Object within an Object - URGENT NEED

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic