• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interfaces and strictfp

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably on the border of what is/isn't necessary knowledge wise for the exam, but is there a simple explanation of why strictfp can be used in an interface definition, but not a method definition within the interface?

i previously assumed it was because there was no implementation to designate as 'strict', so to speak, but doesn't defining the interface strictfp implicitly do this anyway?

i'm happy with a 'that's just the way it is' answer by the way. i've plenty of other things to focus on and I feel this is unlikely to come up! i just thought it was a bit odd!

regards,

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:probably on the border of what is/isn't necessary knowledge wise for the exam, but is there a simple explanation of why strictfp can be used in an interface definition, but not a method definition within the interface?

Yes, there is!

If you have a strictfp interface, all float or double expressions within the interface declaration will be explicitly FP-strict. This implies that all nested types declared in the interface are implicitly strictfp. So you can have an interface like thisSo all concrete methods of the class NestedType will be be explicitly FP-strict. And we can prove this using some reflectionOutput:
Method Name: abstractDoIt --> Modifiers: public abstract
Method Name: doIt --> Modifiers: public strictfp


So as you can see the concrete method doIt of class NestedType is strictfp, although strictfp was only used on the interface. And the abstract method abstractDoIt of class NestedType does not have the strictfp modifier. The reason is simple: strictfp tells you something about the implementation of a method, but an abstract method does not have an implementation.
That's also the reason why strictfp is not allowed on methods defined in an interface in Java 7: all methods defined in an interface are inherently abstract. In Java 8 you'll have default and static interface methods, these methods actually have an implementation so these methods can have the strictfp method. For abstract interface methods in Java 8 the same rule applies as in Java 7: the strictfp modifier is not allowed.

The same applies to the synchronized modifier as well: it tells you something about the implementation, so it's not allowed on abstract methods.

Hope it helps!
Kind regards,
Roel

PS1. strictfp and synchronized are not on the OCA exam (except that both are Java keywords and thus not allowed as identifiers)
PS2. Nested types are not on the OCA exam! I used it for demonstration purposes only. Much more on nested types when you prepare for the OCP exam
PS3. Reflection is not on the OCA exam nor on the OCP exam. I used it for demonstration purposes only.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic