Java: How to limit access of a method to a specific class?

class, java

Solution

I would pass the object that is calling the method as an argument, i.e.

list.insert("x", this);

And then check if the passed Object is an Instance of Class A

 public void insert (String x, Object o)
   {
      if(o instanceof ClassA){
         /*insertion occurs*/
      }
   }

Problem

Here's an example: ``` class A { List l = new List (); list.insert("x"); } class List { ... public void insert () { /*insertion occurs*/ } ... } ``` Is it possible at all to keep the insert() method public, but limit access only to class A so that no other class can access it, only when called from A?

Original source