Results 1 to 7 of 7

Thread: overwriting classes in loops

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    overwriting classes in loops

    hey ya'll!
    what happens if:

    Date d = new Date();
    StringTokenizer st = new StringTokenizer();

    ...both of these lines are in a while loop... what would happen when the loop is passed the second/third etc time?

    with variables, if you declare them outside the loop, their values will be overwritten throughout the loop. but what about those two? i don't mind having "d" (in Date) overwritten; it's just that at every pass in the loop, i need to pass different arguments into it. ie.

    1st pass: Date d = new Date(2003, 12, 12);
    2nd pass: Date d = new Date (2001, 6, 6);

    would declaring them in the loop be okay or would i have to do so before the loop? and if the latter is true, how do i pass new arguments into "d" if the constructor line is outside the loop?

    eh?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It will create an incredible amount of objects floating around.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    any way to "clear" them in code?

  4. #4
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    CornedBee
    wouldn't the garbage collector take care of the objects that were created in the loop for each iteration? So after the first iteration the objects are removed...

    quipy
    In the Date class you have methods for setting year, month and day, so you don't need to create a new date object in each iteration. Just change the values of a date object already defined outside the loop.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The problem isn't memory consumption, the garbage collector takes care of that. The problem is the overhead of creating objects and collecting them.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Im pretty sure the the Garabage Collector runs in a low priority background thread so there is no guarantee that the objects will get collected in the order that they were created or at all for that matter. Automatic garabage collection should not be preceived as a license for uninhibited creation of objects and forgetting about them. Im sure CornedBee would agree.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Absolutly. The garbage collector runs when there's nothing else to do, when memory gets low or when it is explicitly invoked using System.gc();
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width