On Jan 24, 4:57 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 21 Jan 2008 20:01:50 -0800 (PST), Jebel.S...@gmail.com wrote,
> quoted or indirectly quoted someone who said :
>
> >There is a class A in package XX.ZZ.GG , and A has a static method
> >A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
> >How can I make the A.GetInstance() only can be invoked by the B, C and
> >D.
>
> Please follow the caps conventions. Seehttp://mindprod.com/jgloss/codingconventions.html
>
> Your question translated then becomes:
>
> >There is a class A in package xx.zz.gg, and A has a static method
> >A.getInstance(). There are 3 classes B, C, D in package xx.zz .
> >How can I make A.getInstance()so that it can only be invoked from
> >classes B, C and D.
>
> You can't. To do that classes A, B, C and D would have to be in the
> same package, and getInstance would need default package scope. You
> could still invoke it from class A.
>
> Note that, as far as Java is concerned, package xx.zz.gg and package
> xx.zz are TOTALLY unrelated.
>
> seehttp://mindprod.com/jgloss/package.html
> --
> Roedy Green, Canadian Mind Products
> The Java Glossary,http://mindprod.com
I know that "package xx.zz.gg and package xx.zz are TOTALLY unrelated
".The names of the two packages are no meaning for my problem.
The classes A and E are in the package XX.ZZ.GG; and the classes B,
C and D are in the package XX.ZZ.
The class E extends from class B.
When the class C wants to invoke the E.GetInstance()(has the same
function of the previous A.GetInstance) it invokes B.e.GetInstance().
package XX.ZZ;
import XX.ZZ.GG.A;
public class C
{
public void Func()
{
... ...
A a = B.e.GetInstanc();
... ...
}
}
The 'e' is a static member of class B, and 'e' is declared as B but
construct as E:
package XX.ZZ;
import XX.ZZ.GG.E;
import XX.ZZ.GG.A;
public class B
{
static B e;
static{
e = new E;
}
protect A GetInstance(){ return null;}
... ...
}
And the class E and class A are in the same package, E can do some
thing in its method GetInstance with A.
package XX.ZZ.GG;
import XX.ZZ.B;
final public class C extends B
{
private static A onlyOne;
protect A GetInstance()
{
if(onlyOne == NULL){ onlyOne = new A;}
return onlyOne;
}
}
Class A :
package XX.ZZ.GG;
public class A
{
// the access right of constructer of A is
default( the class in the same package can access)
A(){... ...}
... ...
}
That is all.