I think I can find exceptions to that rule.Tim Moores wrote:. . . all objects . . . are worthless unless you have a reference to it. . . .
Crystal Zeng wrote:For example, a.add(new ***()).
Carey Brown wrote:
Crystal Zeng wrote:For example, a.add(new ***()).
"an object has no point unless somebody has a reference to it"
In this case 'a' (I'm guessing is a list) gets the reference when add() is called.
an object has no point unless somebody has a reference to it
Carey Brown wrote:for instance creates a string object and returns a reference but because we're not assigning the reference to anything the program will forget about this String reference.
Creates a String but this time assigns it to the variable 's'. So as long as 's' is alive (i.e. doesn't go out of scope) the program will remember the String reference.
Now the reference is assigned to 's' and then 's' is passed as a parameter to add(). If 's' goes out of scope it doesn't matter because the collection 'a' now has a copy of it. In this case 's' was just a temporary name we gave it so that it made human readable sense when we passed it to add(). We didn't need to give it a name at all if we just gave it directly to add().
Here we pass it directly to add(). The end result is the same as the preceding example but we don't have to give it a named reference.
Paul Clapham wrote:
an object has no point unless somebody has a reference to it
This isn't really true. You can create a reference to an object like this:
Now, right now nobody (i.e. no variable) has a reference to the object... but that doesn't stop you from using the object. Let's suppose our Something class has a public method named dump() which dumps some information to the console. Then you can call that object's dump() method like this:
and you'll see that information being dumped to the console. Notice that no variables were used to make that happen.
Crystal Zeng wrote:According to the attachment below, new ***() means creating an object, it seems not to refer to a reference. What's more, when saying new, the constructor runs, but the constructor return nothing , doesn't it?
The Java Tutorial wrote:The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory
The diagram with the Dog shows how Java® execution goes from left to right:-Junilu Lacar wrote:. . . new is an operator and has a relatively high order of precedence . . .
That is another form of what I called “use it immediately”.Paul Clapham wrote:. . . Let's suppose our Something class has a public method named dump() . . .. . . .
Campbell Ritchie wrote:
I think I can find exceptions to that rule.Tim Moores wrote:. . . all objects . . . are worthless unless you have a reference to it. . . .
Careful: the OP is a newbie and this may all be going over her head.Junilu Lacar wrote:. . . it's more consistent with the idea that a constructor returns nothing . . .
Stephan van Hulst wrote:If we're being pedantic, ... So really, the constructor doesn't return anything, it's called on an object that already exists.
Campbell Ritchie wrote:
Careful: the OP is a newbie and this may all be going over her head.Junilu Lacar wrote:. . . it's more consistent with the idea that a constructor returns nothing . . .
Why should a constructor return anything? Constructors aren't methods. ... it would be better to say that a constructor doesn't have a return type. Not even void.
Crystal Zeng wrote:
OP (or somebody): please tell us the page numbers in HFJ too. That will make it easier to find the section you are discussing
Crystal Zeng wrote:For example, a.add(new ***()).
When using this object as a arguement directly , how is it copyed and passed into the parameter? Why doesn't the object need to be assigned to a reference firstly and then use its reference as the arguement(like *** b=new ***(), a.add(b))?
The description saying an object's life has no value , no meaning , no point.... inspired me to come out with this question.
Crystal Zeng wrote:My confusion comes from the mind that I always think of new **() as an object and an object isn't allowed to be used directly.The book called <Head First Java>never says new operator will return a reference.From the descriptions below, I tend to think of the right side of = operator as an object.
All things are lawful, but not all things are profitable.
Crystal Zeng wrote:My confusion comes from the mind that I always think of new **() as an object and an object isn't allowed to be used directly.The book called <Head First Java>never says new operator will return a reference.From the descriptions below, I tend to think of the right side of = operator as an object.
I couldn't see a page number. There was an image which didn't display properly and nothing else.Crystal Zeng wrote: . . . Page here.
Campbell Ritchie wrote:
I couldn't see a page number. There was an image which didn't display properly and nothing else.Crystal Zeng wrote: . . . Page here.
Don't get me started about those stupid light bulbs. |