• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java 17 OCP: why is anonymous class called anonymous

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Earlier Java 8 was this a thing what I don't understand.

Normally straightforward you make an instance of class:

right, the type is ClassX, reference name classX, but there is not a class name, or did I already missed something?


sale is Local variable in admission of the type SaleTodayOnly.

What is meant by anonymous class doesn't have a name? The abstract class does have a name:  "SaleTodayOnly".
The situation that a class does have name is that meant by: ?
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first code snippet, ClassX is a concrete class. Its name is "ClassX".

In your second code snippet, SaleTodayOnly also has a name: "SaleTodayOnly". However, that's not the concrete type that you're instantiating on line 8. After all, you can't directly instantiate an abstract class.

Instead, you're instantiating a concrete subclass of SaleTodayOnly. That concrete subtype doesn't have a name. In other words, its anonymous.

Try adding a constructor to the body of your anonymous class. You can't, because what would the name of the constructor be?
 
Nico van de Kamp
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan, it's maybe stupid but it is still hard for me to understand.
I know that it is not possible to instantiate an abstract class, but how does Java interpret\determine,  to instantiate a subclass?

If I change it to a concrete class like this:


Now it is a concrete class instantiating, which has a name and is assigned to a local variable. Why is it still called anonymous? How can I compare with what have name in an other situation and now not?

Maybe stupid, what do I not see?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nico van de Kamp wrote:Now it is a concrete class instantiating, which has a name and is assigned to a local variable.


No. You're still instantiating an anonymous class. This time it's a nameless subclass of SaleTodayOnlyConcreteClass.

When you add a class body next to a constructor call, that syntax is what determines that a nameless subclass is defined.
 
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your IDE (in my case NetBeans) can illustrate this nicely. I use an interface for brevity.

If you ask the IDE to replace the vars with their real types, then j and s are reported as I and Supplier<Integer>, but it does not give types for k and t. Meaning that their real types are unknown? k instanceof I is true, by the way.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic