Results 1 to 4 of 4
  1. #1
    deleyd is offline Member
    Join Date
    Jun 2011
    Rep Power

    Default Why is it OK to create this without assigning a reference to it?

    In the following sample code I got from a book on Java, my question is about line 30:
    Java Code:
          new Agent(name);
    We're creating something here, but we aren't saving a reference to this new thing we just created.

    I have this apparently mistaken idea that when you no longer have a reference to something the garbage collector will deallocate it. Why doesn't that happen here? When is this true and when is this not true?
    Java Code:
    * Dean & Dean
    * This program manages find-raising agents.
    import java.util.Scanner;
    public class FundRaiser
      public static final double GOAL = 10000.00
      public static void main(String[] args)
        Scanner stdIn = new Scanner(;
        int numberOfAgents;
        double totalValue;
        String name;
        System.out.print("Enter total number of agents: ");
        numberOfAgents = stdIn.nextInt();
        for (int i=0; i<numberOfAgents; i++)
          System.out.print("Enter agent name: ");
          name = stdIn.nextLine();
          new Agent(name);
          totalValue = Agent.getAllValues();
          System.out.printf("Total value = $%,.2f\n", totalValue);
        } while (totalValue < GOAL);
        System.out.println("Time to Spend!");
      } // end main
    } // end FundRaiser class
    (I'd post the rest, but shouldn't encapsulation be such that we shouldn't have to dig into what goes on underneath in order to understand what's going on at a high level?)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power


    It very well could be garbage collected, unless it's being added to some kind of a Collection (and therefore a reference to it still exists). We can't see what's happening in Agent, so we can't really tell you what's going on.

    But yeah, throwaways like that happen all the time. You probably do it yourself without even knowing it. For example, that System.out.printf() is returning a PrintStream (check the API if you don't believe me), but you don't do anything with it. There's no rule saying you have to store something in a reference variable, and sometimes that's very useful.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Rep Power


    Without looking at the Agent class it's hard to think why such a call would be made, but one possible reason I can think of is that the constructor passes a reference to the instance to some other object:

    Java Code:
    class Agent {
        static ArrayList<Agent> instances = new ArrayList<Agent>();
        public Agent() {

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Camp Lejuene, North Carolina
    Rep Power


    Java Code:
    Either you can call parts of Agent with it being instantiated or it references itself when you construct it. You can make objects automatically perform certain tasks when you instantiate them. If you haven't learned about that yet, don't worry it becomes much more clear later.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Assigning labels to JSliders?
    By Vase in forum New To Java
    Replies: 5
    Last Post: 04-06-2011, 12:37 AM
  2. Need some help with assigning & Exceptions
    By Jubic in forum New To Java
    Replies: 7
    Last Post: 06-07-2010, 06:53 AM
  3. How to create pass by reference??
    By --> xeiyne! in forum CLDC and MIDP
    Replies: 4
    Last Post: 04-08-2010, 07:43 PM
  4. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 04:13 AM
  5. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 07:30 AM

Posting Permissions

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