> Hi,
>
[quoted text clipped - 3 lines]
> class in P can access the public methods of A. That's fine because I
> need the other classes in P to be able to call those public methods.
Every class in P,Q,R,S,... can call the public methods of A.
The meaning of `public' is "Anybody at all can see me/call me."
> class B is also part of package P. I want A and B to be the only
> classes that can instantiate objects of class A. How do I achieve
> this? Is this even possible?
If A and B are the *only* classes in P, then specify no
access at all for the constructors of A: don't say `public',
don't say `protected', don't say `private'. The default is an
access often called "package-private," meaning "visible only
to classes in the same package." So if A and B are the only
classes in P, then A and B will be (ipso facto) the only classes
that can call A's constructors.
> I known one solution is to define another package Q and stick A and B
> in Q and give A protected constructors. But I want to know if it's
> possible without creating another package.
You misunderstand `protected'. It means "accessible from
any code in this class (like `private') plus any code in the
same package (like "package-private") plus any code in any class
anywhere that happens to extend A."
That is, `protected' is *less* "protected" than nothing.

Signature
Eric Sosman
esosman@ieee-dot-org.invalid
> I want A and B to be the only
>classes that can instantiate objects of class A
There are three levels of access: the class, the package and public.
The only way I could think of you could arrange that is to put A and B
in the same package, give the constructors default (package) scope,
and put no other classes in that package.
See http://mindprod.com/jgloss/scope.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Eric Sosman - 18 Jul 2007 13:28 GMT
>> I want A and B to be the only
>> classes that can instantiate objects of class A
>
> There are three levels of access: the class, the package and public.
And protected. There are four levels of-- wait, I'll
come in again.

Signature
Eric Sosman
esosman@ieee-dot-org.invalid
Lew - 18 Jul 2007 14:13 GMT
>>> I want A and B to be the only
>>> classes that can instantiate objects of class A
[quoted text clipped - 3 lines]
> And protected. There are four levels of-- wait, I'll
> come in again.
An almost fanatical devotion to James Gosling (or, in my case, Joshua Bloch), ...

Signature
Lew
Eric Sosman - 18 Jul 2007 15:56 GMT
Lew wrote On 07/18/07 09:13,:
>>>>I want A and B to be the only
>>>>classes that can instantiate objects of class A
[quoted text clipped - 5 lines]
>
> An almost fanatical devotion to James Gosling (or, in my case, Joshua Bloch), ...
NO one expects the Java Disquisition!

Signature
Eric.Sosman@sun.com
David Breton - 20 Jul 2007 00:34 GMT
>> I want A and B to be the only
>> classes that can instantiate objects of class A
[quoted text clipped - 6 lines]
>
> See http://mindprod.com/jgloss/scope.html
Thanks,
That was the answer I had in mind too but as I mentioned in my
original post, I did not want to create a new package. So what I
ended up doing was to define A as an inner class of B and I gave A a
private constructor. Now only A and B can instantiate objects of
class A.
The downside is that for all the other classes in the package I have
to refer to object of class A by their full name (i.e. P.B.A myA =
methodThatReturnAnObjectOfClassA(); or to use 'import P.B.A;' in every
class that needs A) in order to use objects of class A but I can live
with that.
David
Ian Shef - 24 Jul 2007 21:20 GMT
<snip>
> That was the answer I had in mind too but as I mentioned in my
> original post, I did not want to create a new package. So what I
[quoted text clipped - 9 lines]
>
> David
Have your package define an interface (call it C) that A implements. An
interface cannot define a constructor, so there is no impact on the
protection that you have set up with A and B.
Now, your other classes can refer to an object of class A via:
C myA = (C)methodThatReturnAnObjectOfClassA() ;
or better still:
C myC = methodThatReturnsAnObjectOfInterfaceC() ; // Note that the return
// type has changed.
Programming to an interface rather than to an actual class has other
advantages as well.
Admittedly, I have not tested this myself.
Good Luck!

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *