Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

class

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?

like image 602
IDDQD Avatar asked Jul 18 '12 20:07

IDDQD


1 Answers

The best you can do using access modifiers is to make the method package private (remove the public keyword) and keep only those two classes in the same package.

like image 193
hvgotcodes Avatar answered Oct 05 '22 23:10

hvgotcodes