• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling method as part of creating a new object?  RSS feed

 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am reading a tutorial on exceptions.

http://tutorials.jenkov.com/java-exception-handling/exception-handling-templates.html

I am rather confused what is going on here. Below is the code, can someone please explain that is happening? This is the bit I am confused about:



We seem to be creating an object without actually giving it a name, and then calling a method on that object, but then we are also defining the method too? This is rather confusing. Please help me understand this or direct me to some page where I can learn about this syntax.

Class InputStreamProcessingTemplate below:


 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is anonymous object. In this you don't have to specify the variable name. Simplest example could be using Runnable interface.




Here you can see, i make anonymous object of runnable.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An anonymous object of an anonymous class, actually.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When did the phrase "anonymous object" became part of the vernacular? Objects shouldn't know if they are referred to by reference variables, or by internal temporary references used during the processing of expressions. Is this term officially defined anywhere?

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it isn't and as you suggest, Henry, it should probably be put out of its misery PDQ.
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, I now went and read about anonymous classes here ...

https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#declaring-anonymous-classes

... and now it makes sense.

Just out of curiosity, have anonymous classes always been around? I did the SCJP in 2006, and I don't remember coming across them then.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they were introduced in Java1.2 which was about 1998.
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think they were introduced in Java1.2 which was about 1998.


Ah, I must have forgotten about them then.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!