• 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

Trick question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We had a discussion recently about a trick question about interfaces. One person asked, "Is it possible to have executable code in a java interface?" The first thought would, be no, since we know what interfaces are meant to be. But on second thought, what about constant declarations? You could do this:

static final int i6 = (int)(Math.random()*40);

This can't be known at compile time, so it is not like it will be optimized into some constant pool and "inlined" like constants by a C preprocessor. So the answer is yes... ???
 
Marshal
Posts: 79464
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it? I have and I know what happens!!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kris Lightsey:
We had a discussion recently about a trick question about interfaces. One person asked, "Is it possible to have executable code in a java interface?" The first thought would, be no, since we know what interfaces are meant to be. But on second thought, what about constant declarations? You could do this:

static final int i6 = (int)(Math.random()*40);

This can't be known at compile time, so it is not like it will be optimized into some constant pool and "inlined" like constants by a C preprocessor. So the answer is yes... ???




A constant can only be inlined if its value is known at compile time. In this case, the value can only be determined and assigned at runtime, so it won't be inlined.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also a second way to put executable code inside an interface - completely unrelated to this method.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what is the compiled code for the specified statement:



So, the compiler does not change anything.

[Jim]There is also a second way to put executable code inside an interface - completely unrelated to this method.



Yeah, you can write an entire class definition inside an interface!
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or static initializer blocks. Or almost anything that isn't an instance variable or non-abstract method. Non-static initializer blocks probably aren't allowed though.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
Or static initializer blocks. Or almost anything that isn't an instance variable or non-abstract method. Non-static initializer blocks probably aren't allowed though.



Static initializer blocks and non-abstract methods also are not allowed inside an interface.
 
Rob Spoor
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nitesh Kant:


Static initializer blocks and non-abstract methods also are not allowed inside an interface.


Well I knew about the non-abstract methods, but the static initializer kinda surprises me - it is part of the class, not of an instance.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

Well I knew about the non-abstract methods, but the static initializer kinda surprises me - it is part of the class, not of an instance.



An interface does not get instantiated unless a variable that does not hold a constant value is used. Thus, a static-initializer execution in an interface can not be guaranteed and so it is not allowed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic