• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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: 499
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: 499
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: 80645
473
  • 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: 42135
937
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic