• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginner / Object Creation

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

my goal is to create say 100, small fillable rectangles

if I put, inside a "for" loop

GRect myRect = new GRect( .........etc

I can create the rectangles

my question is,is this correct, ie: should each rectangle be a "new" GRect, or is there a way to just make copies of the first GRect?

thanks
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can create 100 Rectangles by saying new inside a for loop.
Copying is likely to be more complicated than simply writing new.

There is a problem here, however; you are declaring and creating the Rectangle in the same statement inside the loop. Unless you have a reference to attach the Rectangle to (eg a Rectangle[]), in which case the declaration is unnecessary, your Rectangles will go out of scope at the end of the loop and vanish into cyber-limbo never to be seen again!

Another thing lots of people try (instead of copying) is something like this rect2 = rect1;. Try it and see what happens to rect2 when you fill rect1.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check-out the flyweight pattern. just depends on what you are doing with the objects.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my question is,is this correct, ie: should each rectangle be a "new" GRect, or is there a way to just make copies of the first GRect?



Creating a copy of the object is slightly complicated where in one has to override the clone method and use it for creating a copy. Actually there are two versions: One is to use another reference variable to point to the object created in which case its not copying and the other version is a new reference variable pointing to the clone object.

In your loop as and when you create Rectangle objects they are eligible for garbage collection after the end of the each iteration because there is no reference to access the object as the same reference will now be pointing to the new object on the heap.
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic