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 New object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "New object creation" Watch "New object creation" New topic

New object creation

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
I have a problem in creating objects.I have a userDefined object with me.I need to take it out and create a new object pointing to a new memory.I am doing this in a loop.So everytime when I came back to the loop.I should have the object I have created with me and create a new object of the same with my desired values.Can anyone please help me out in this.
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
what do you mean take it out? is it on a file or in memory? please be more specific, better still paste your code and explain what you want to do, that should help us help you better

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You create a new object using the new keyword. For example:

I'm sorry that I cannot be more specific, but you haven't given me much to work with. Are you trying to create objects from a custom class or from the Java API? It works the same with both of them, but I'm just curious if you have written your own class yet. Also, it will help if you can give us some code that you have already tried. Code will sometimes illustrate what you want to do more clearly than words do.

In addition, what are you doing with these objects? Are you putting them into an array or a Collection? Or is each object a one-time use for each iteration through the loop? Some more details along these lines will greatly help us to be able to answer your question.


Java API Documentation
The Java Tutorial
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
LevelSpec levelSpecToReplace=pdfSpec.getLevelObject(levelToReplace);
ArrayList filterSpecsOfReplaceLevel = (ArrayList)levelSpec.getFilterObjects();
for (Iterator filterSpeciterOfReplaceLevel = filterSpecsOfReplaceLevel.iterator(); filterSpeciterOfReplaceLevel.hasNext() {
FilterSpec filterSpecOfReplaceLevel = (FilterSpec);
String filterNameOfReplaceLevel=filterSpecOfReplaceLevel.getFilterName();
ArrayList filterSpecs = (ArrayList)levelSpecToReplace.getFilterObjects();
for (Iterator filterSpeciter = filterSpecs.iterator(); filterSpeciter.hasNext() {
//FilterSpec filterSpecOfNewPdfObj = (FilterSpec)((FilterSpec);
FilterSpec filterSpecOfNewPdfObj = new FilterSpec((FilterSpec);
String filterNameOfNewPDfObj=filterSpecOfNewPdfObj.getFilterName();
PdfSpec pdfSpecNew=new PdfSpec();
String filtValue=keyStr;
boolean b=false;
PdfGeneratorUtil pdfGeneratorUtil=new PdfGeneratorUtil();
ArrayList filterSpecsForPopulatedFilter=new ArrayList();
PdfGenerator genPdf = new PdfGenerator();
genPdf.startPdfGen(pdfSpecNew, null, true);

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
Here whats happening is whenever I checks for the "?" its getting replaced with the previous value inspite of creating the new object.Could you pleas help
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Hey Renu, please use the UBB tag "CODE" for listing Java codes, then you code will be much clear in the posting. For example:

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
what is UBB code?I didnt get you?
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
Hi friends,
I got the solution for my problem.Even if I create a new ArrayList the members inside it will point to the same memory as the previous ArrayLIst.So I tried looping through the members in the arrayList and created a new object of each.Hope this will help someone in the future
I agree. Here's the link:
subject: New object creation
jQuery in Action, 3rd edition