File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

New object creation

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
HEllo,
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.

Layne


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) filterSpeciterOfReplaceLevel.next();
String filterNameOfReplaceLevel=filterSpecOfReplaceLevel.getFilterName();
ArrayList filterSpecs = (ArrayList)levelSpecToReplace.getFilterObjects();
for (Iterator filterSpeciter = filterSpecs.iterator(); filterSpeciter.hasNext() {
//FilterSpec filterSpecOfNewPdfObj = (FilterSpec)((FilterSpec) filterSpeciter.next()).clone();
FilterSpec filterSpecOfNewPdfObj = new FilterSpec((FilterSpec) filterSpeciter.next());
String filterNameOfNewPDfObj=filterSpecOfNewPdfObj.getFilterName();
if(filterNameOfNewPDfObj.equals(filterNameOfReplaceLevel)){
PdfSpec pdfSpecNew=new PdfSpec();
String filtValue=keyStr;
boolean b=false;
if(filterSpecOfNewPdfObj.getFilterValue().equals("?")){
b=true;
filterSpecOfNewPdfObj.setFilterValue(filtValue);
}
PdfGeneratorUtil pdfGeneratorUtil=new PdfGeneratorUtil();
keyStr=pdfGeneratorUtil.fixupName(keyStr);
pdfSpecNew.setName(keyStr);
pdfSpecNew.setBookFileName(keyStr);
ArrayList filterSpecsForPopulatedFilter=new ArrayList();
filterSpecsForPopulatedFilter.add(filterSpecOfNewPdfObj);
levelSpecToReplace.setFilterObjects(filterSpecsForPopulatedFilter);
pdfSpecNew.setLevelObjects(pdfSpec.getLevelObjects());
pdfSpecNew.setLevelObject(levelToReplace,levelSpecToReplace);
PdfGenerator genPdf = new PdfGenerator();
genPdf.startPdfGen(pdfSpecNew, null, true);
if(b==true){
filterSpecOfNewPdfObj.setFilterValue("?");
}

}
}
}
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:



SCJP, SCWCD
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: http://aspose.com/file-tools
 
subject: New object creation