Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Sybex CSG 11 - page 523 - rules for private interface methods

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

In CSG 11 on page 523 it says:

2. A private interface method may be called only by default and private (non-static) methods within the interface definition.



I believe part about "non-static" might be misleading since the following code is working for me:



Could you please verify if I am right here?
 
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michał Pelc wrote:Could you please verify if I am right here?


Michał,
As you know, it compiles, runs without exception, and prints "Doing it" for me, as well.

Michał Pelc wrote:I believe part about "non-static" might be misleading


Michał,
I really struggled to come up with something that explains why Rule #2 makes sense 100% of the time.  

With that said, as you may already know, from page 268 your example appears legal because

A static member cannot call an instance member without referencing an
instance of the class. This shouldn’t be a surprise since static doesn’t require any instances
of the class to even exist.



Obviously, we have the required cat instance in your example.  It does appear that this rule seems to have a "precedence" over Rule #2.  As you know, that section of the book text does not appear to have an example as elaborate as yours.  Sorry that I couldn't be more help.  

By the way, I once asked a question somewhere ... essentially the answer was something to the effect of (heavily paraphrasing) "It's a book ... you can't take a single sentence and essentially forget the other sentences in the whole book."  Anyway, thanks for the question.  

I am looking forward to what others have to say on this!  
 
Charles O'Leary
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

If you go through the JLS (-Java® Language Specification), it tells us under which circumstances we may or may not call an interface method private. It doesn't mention any differences in access in that section; that suggests to me that access is the same as it would be anywhere (JLS link).

JLS§6.6.1 wrote: . . . Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level type (§7.6) that encloses the declaration of the member or constructor. . . .

I shall leave you to go through the analogous analogous JLS section about class members to see what it says about access there. What you are doing is not accessing an instance member in a static method,, but passing a reference from which all accessible members can be found. It is analogous to the suggested form of a main() method in Winston's FAQ, or to writing this:-I tried your code on JShell (Java15) and it ran first time:-

Campbell's JShell wrote:interface Cat {
   static void checkIfDoable(Cat cat) {
       cat.doIt();
   }
   
   private void doIt() {
       System.out.println("Doing it");
   }
}|  Welcome to JShell -- Version 15
|  For an introduction type: /help intro

jshell> interface Cat {
  ...>         static void checkIfDoable(Cat cat) {
  ...>                 cat.doIt();
  ...>         }
  ...>          
  ...>         private void doIt() {
  ...>                 System.out.println("Doing it");
  ...>         }
  ...> }
|  created interface Cat

jshell> public class Scratchpad implements Cat {}
|  created class Scratchpad

jshell> Cat.checkIfDoable(new Scratchpad());
Doing it

jshell>  

I presume you have checked the errata; I didn't find this issue.
 
author & internet detective
Posts: 40722
827
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That list means directly. So you can't do this:


Just like a static method can't call an instance method without an instance reference in a regular class.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic