Results 1 to 4 of 4
- 06-22-2011, 06:44 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
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:
We're creating something here, but we aren't saving a reference to this new thing we just created.Java Code:new Agent(name);
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?
(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?)Java Code:/****************************************************************** * FundRaiser.java * 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(System.in); int numberOfAgents; double totalValue; String name; System.out.print("Enter total number of agents: "); numberOfAgents = stdIn.nextInt(); stdIn.nextLine(); for (int i=0; i<numberOfAgents; i++) { System.out.print("Enter agent name: "); name = stdIn.nextLine(); new Agent(name); } do { Agent.addAllValues(); 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
- 06-22-2011, 06:51 PM #2
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 - Play indie games, learn from game tutorials and source code, upload your own games!
- 06-22-2011, 06:54 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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() { //... instances.add(this); } }
- 06-22-2011, 06:58 PM #4
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.Java Code:Agent.addAllValues();
- 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
-
Assigning labels to JSliders?
By Vase in forum New To JavaReplies: 5Last Post: 04-05-2011, 11:37 PM -
Need some help with assigning & Exceptions
By Jubic in forum New To JavaReplies: 7Last Post: 06-07-2010, 05:53 AM -
How to create pass by reference??
By --> xeiyne! in forum CLDC and MIDPReplies: 4Last Post: 04-08-2010, 06:43 PM -
Assigning values to an object
By camper2 in forum New To JavaReplies: 4Last Post: 04-05-2009, 03:13 AM -
Assigning a string value to a char
By coffeebean in forum New To JavaReplies: 4Last Post: 06-15-2008, 06:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks