• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Instantiating interface????

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking at a whizlabs question and I do not understand what is happening:



I did not think it was possible to instantiate an interface.  But the answer to what it outputs is IStatic.  I feel like the thing that I am not understanding is how can you instantiate an interface?  I suspect it has something to do with the {} after the instantiation.  I'm fairly certain that this is not a valid question for the OCA, but I don't care about that because I am now trying to prepare for the OCP and I want to understand the concept.

Can someone explain to me how this works?  I've tried googling and everything that I've found thus far says that you can't directly instantiate an interface.

Thank you!!
 
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 13 contains {} and that converts it from an interface to an anonymous class which implements that interface. It just so happens that your interface has no abstract methods, so empty {} is acceptable.

Don't use single letters for class names; reserve the single letters for formal type parameters.
 
Robin Z. Clark
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your response.  It makes sense to me.  And thank you for your response from yesterday too.  It helped.  The code is from the whizlab question.  I would NEVER use such awful identifiers
 
Robin Z. Clark
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd never be able to wrap my head around these concepts if it wasn't for javaranch.  Thank you to ALL moderators!
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robin Z. Clark wrote:Thank you . . . And thank you for your response from yesterday too. . . .

That's a pleasure

I would NEVER use such awful identifiers

Hahahahahahahahaha!
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was somebody else, maybe Carey, who helped you yesterday.
 
Grow your own food... or this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!