• Post Reply Bookmark Topic Watch Topic
  • New Topic

anonymous object  RSS feed

 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some one please explain anonymous objects with example clearly...i read some where anonymous objects advantage is saving memory...it is benificiable when there is only one time object usage in our program..i can't understant one time usage of object ....i know anonymous objects but i don't know in which context we use them in our programs...i did the anonymous object program with my own example but i can't differentiate this one with normal object..i'm providing my own example below
i used more than once but it gives result as normal one
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:some one please explain anonymous objects with example clearly...i read some where anonymous objects advantage is saving memory...it is benificiable when there is only one time object usage in our program..i can't understant one time usage of object ....i know anonymous objects but i don't know in which context we use them in our programs...i did the anonymous object program with my own example but i can't differentiate this one with normal object..


An object doesn't differentiate either. It doesn't really know how many references are pointed to it (until it gets GC'ed), nor does it know if its class definition is in scope or not.

So... not sure of the advantages that you are referring to. In fact, IMO, if you are constantly creating objects, just to use once, and throw them away, that seems like a disadvantage over creating an object once, and using it over and over again. How is creating lots and lots of objects, and taxing the garbage collector an advantage?

Henry

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An anonymous class isn't a class named "anonymous", it's a class that has no name and is build "on the fly", so to speak.



Here we've created an anonymous class with one method, run(). It's as like we had a file like this:



...except the class is not RunMe, it's anonymous, that is, it has no name.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful about nomenclature. There are such things as anonymous classes and there are such things as anonymous objects. The two are not the same
BufferedReader inRead = new BufferedReader(new FileReader(new File("myFile")));
That old‑fashioned way to read a file uses two anonymous objects (bits of code underlined) but they are created from named classes.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Careful about nomenclature. There are such things as anonymous classes and there are such things as anonymous objects. The two are not the same.


Thanks for the clarification. I didn't know anonymous objects could be make from named classes. But thinking about it, I've written code like that!
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarification:

An anonymous object is an object of a named class without a name... when you write a statement like this -> String myString = new String("Java");... The compiler creates a String object and gives it the name myString which acts as a handle to refer to that object...

Whereas if you write a statement like this -> new String("Java");... The compiler creates a String object but as you can see it wasn't given a name so there is no way to refer to it as it has no name, its anonymous...

An anonymous class is an implementation of some type (an interface or an abstract class) that is inferred by the compiler from its context... e.g -> Runnable runner = new Runnable() { public void run() {} }; ... From this you can see that the implementation of the interface was created without given the implementation a name so its anonymous...

As opposed to -> public class MyRunnable implements Runnable { public void run() {} } ... Runnable myRunner = new MyRunnable(); ... From this the implementation of the interface was given a name MyRunnable so its a named class...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:
An anonymous object is an object of a named class without a name... when you write a statement like this -> String myString = new String("Java");... The compiler creates a String object and gives it the name myString which acts as a handle to refer to that object...

That is not a very good way to think about it, and is not accurate.

Objects do not have names, and names are not associated with objects. In the code example given, a variable named myString is created which is of type String, so it can refer to a string object. The assignment operator assigns a reference to the newly-created string object to the variable. In no way, shape, or form is the name myString associated with the object itself.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read that in a book somewhere so I only explained what I learnt where they used the notation myString:String

If a key is associated with a value why can't a variable name be associated with an object?...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now, what "name" is associated with the string object?

Regardless of what you read, a variable has a name; objects do not. The object has no association with variable names; the variables refer to the objects, not the other way around.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fully understand your point and it make sense to think of it in that way also...

To me I believe it can also make sense to think of it as being named, as an object is just a region of memory of some type and value... and a variable is a named object which mean it provides a name for that region of memory...

You are an object and you have a name that was given to you at birth, but people can also call you by other names that wasn't given to you at birth making it an alias... By mentioning any of the familiar names that are attached to you, you will respond... This is the same simulation within the computer system
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I cannot agree. I think it misleading and inaccurate to think of the object as having the name of a variable that just happens to reference it. The birth name analogy is not analogous or appropriate.

The "region of memory" is not named. A named variable temporarily references the "region of memory".

You avoided answering my first question. If you are going to think of objects as "named", what happens when two variables point to the same object? What about one hundred variables?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Computer hardware deal with memory addresses to store and locate data... When you want to store some value it must determine where to place it and how it can be located which at the lowest level deal with actual numbered addresses...

When you declare a variable such as int number; ... it can be translated into assembly code as number sdword ? ... The memory space is given a name allowing you to use that name to refer to the address space...

By given the same address space a different name is just providing an alias for the same address space: int & newName = number; -> number, newName sdword ? ... This is the way I see it
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are, of course, free to see it as you will. But I do not think it is a good way to model it in your mind and certainly do not recommend that people trying to understand how Java works use this model of thinking of it.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you on saying it is not a way for people trying to understand Java not use this model, that's absolutely correct as the purpose of the language is to abstract these details providing a much more productive environment... I was just saying that its not incorrect to view it this way
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to belabor the point, but I think it is incorrect.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear is right; if you use sloopy nomenclature you will start to believe yourself and you are at risk of sloppy programming. And that is potentially dangerous.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK... Since you'll are knowledgeable professionals with way more experience than I do, I'll accept that you'll say to think of a variable name as just a handle used to reference an object and that the object itself doesn't have a name...

I just always challenge things before I accept them blindly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!