• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OCP 17 - question from practice tests

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I made a mistake on the following question, and the answer confuses me a lot. The question is:

Given the following program, what is the first line to fail to compile?


I thought that the first line not to compile is line 4, because it references Power interface (which is instance nested type) from the static method, and that is wrong. Then I tried and indeed that's not the problem. When I replaced Power interface with a class that is extended on line 4, then I get the compiler error I thought I would get in the first place. Why is that?

Kind regards, Amer
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amer, welcome to CodeRanch.

Inner interfaces are iplicitly static but inner classes are not. That's why you are able to access interface Power inside the main method but not if you change it to a class
 
MyExamCloud Software Support
Posts: 734
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first line to fail to compile is actually line 7, the declaration of the "end" variable. This is because the "static" keyword cannot be used in a local class declaration, and since it is used in the anonymous class creation, it will fail to compile.

The reason why line 4 did not fail to compile initially is because it is within the main() method, which makes it a local class, not an instance nested type. This means that it does not have access to the static variables of the enclosing class, and can only access them through an object reference. However, since the "end" variable does not use the "static" keyword, it does not attempt to access the static variable "t" and therefore it does not cause a compilation error.
 
Saloon Keeper
Posts: 15612
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Inner interfaces are iplicitly static but inner classes are not.


A slight correction. You meant to say "member interfaces" and "member classes".

An "inner class" is a "member class" that is not static. There is no such thing as an "inner interface", because member interfaces are always static.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic