• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trick question  RSS feed

 
Kris Lightsey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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... ???
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? I have and I know what happens!!
 
Jeff Rummings
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also a second way to put executable code inside an interface - completely unrelated to this method.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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!
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!