You can create a Rectangle object with Rectangle rec = new Rectangle(10,10), and then later do something like rec.origin.x and get a value returned. If when you use the new Point(0,0), you no longer are able to access that reference, how does the origin object in rec know where to go? I have used these types of statements several times, but coming across Sun's statement that you lose the ability to access this reference after the statement is done executing through me for a loop (and not the good kind that ends cleanly). Is it due to the fact that when you send the unassigned reference to Point p you are now giving it a variable it is associated with? If you want to see the whole code, it's at: http://java.sun.com/docs/books/tutorial/java/data/objects.html Select the CreateObjectDemo, Point and Object java files. I figure enough of you have been through this tutorial that you know precisely where and what I am talking about Jason [This message has been edited by jason adam (edited July 12, 2001).]
[This message has been edited by Marilyn deQueiroz (edited July 12, 2001).]
I went to the links you included and I don't understand your question. The Rectangle object has a bunch of different constructors. In only one of them do you have to pass in the Point object. If you don't pass in the Point object you get a default Point object with the values being set to 0 and 0. At any point after you have a Rectangle object you can change the Point object it contains by using the "move" method or assigning a new Point object directly.
You can create a Rectangle object with Rectangle rec = new Rectangle(10,10), and then later do something like rec.origin.x and get a value returned.
That is correct and if you have not set the Point object to anything yet you will get 0.
If when you use the new Point(0,0), you no longer are able to access that reference, how does the origin object in rec know where to go?
I guess this is your question that confuses me. If you set the Rectangle to use a new point, with "move" or the assignment, your Rectangle object would have the new Point object values. You would always be able to access the object until it goes out of scope.
Please ignore post, I have no idea what I am talking about.
Chicken Farmer ()
Joined: May 08, 2001
For some reason the quote from their tutorial still is not showing up. The point of my confusion is they state that when you use the new operator to create an object but do not assign it a variable, that after that statement is done executing you no longer can access the object that is created. After reading it over again and working through a few things, I think I understand what they are meaning. I believe that by sending that object to the constructor, you are creating a different reference to that object, and therefore can access it's values after the new Point statement is done. The object is still there, but the reference is not. That was my source of confusion.
If I'm understanding this all correctly, the point is related to scope and whether or not an identifier is used for something you're passing into a method (a constructor in this case). I think the use of a constructor (and constructor chaining) in this specific example makes things a little more confusing than they need to be. Say you have a method that does something with a Point object and two integers. void doSomething( Point p , int w , int h ) ; If your code calling that method looks like this:
In this version, since you declared identifiers for the values you passed into the method, you can still work with point, width, and height after the method call if you need to. Those identifiers will still be in scope. But if your calling code looks like this:
Then you don't have any handle at for any of these values after the method call. A Point object gets created, but you have no way, within the scope of this code snippet, to get at it. (Unless doSomething ultimately does something that results in you getting a handle to the Point object -- in Sun's example you will ultimately have access to the Point object that gets created but that's only because it gets assigned to Rectangle's public field "origin".)
From original post -- You can create a Rectangle object with Rectangle rec = new Rectangle(10,10), and then later do something like rec.origin.x and get a value returned. If when you use the new Point(0,0), you no longer are able to access that reference, how does the origin object in rec know where to go?
origin gets its value from the act of parameter passing during a method call. Given this code from the original post:
If you call this code like this:
In the magic of the method call executing, the values of the actual parameters are assigned to the appropriate formal parameters. It's as if the code that gets executed looks like this:
If the calling code is like this, then the same thing happens:
Is this what you were asking about, or have I made things even murkier? [This message has been edited by Michael Matola (edited July 14, 2001).]
I understand your question better now. Sorry if I am repeating something that someone else has already said. You have an object. Call it object1. You have a reference to object1, lets call it ref1. If for any reason object1 no longer has any references pointing to it, like for example ref1 is now pointing to object2, than object1 will be garbage collected. As soon as an object doesn't have anyone using it anymore it is garbage collected. One of the most common ways for object to be garbage collected is when they go out of scope. So for example if you have a method in which 5 objects are made - when program control leaves that method these objects don't have anyone using them anymore and they are garbage collected. Hope that helps.
Chicken Farmer ()
Joined: May 08, 2001
Nope Michael, that was exactly what I was talking about. Thanks both of you, y'all hit the nail on the head in regards to where I was getting confused. The biggest problem I had was remembering that you pass by reference, so once the reference that is created in the method call is passed to origin, you now have a different reference pointing to that object, so you can now access it with that. Like what Chris said, as long as there is a reference to it, there's an object. Thanks again! Jason