The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Sorry for the long message !! revert back if I have got your question wrong

Posted by Sridhar Visvanath on November 17, 2000 at 1:11 AM

import mylib.*;

class Client
{
public static void main(String[]args)
{
InDirectAccess ia = new InDirectAccess();
/* try uncommenting the following line and you will get a compile-time error
that you can't access the class because DirectAccess is not a public class and
a package class and hence available only within its package.
The public int in DirectAccess carries any meaning
only if the class to which it belongs , in this case DirectAccess , has a public access,
which is not true as DirectAccess is available only within it's package mylib

The access specifiers for classes supersedes that of its variables.

When you cannot enter a country (say USA) how will you be able to
enter a city (say Buffalo) in that country.

similarly
When you cannot enter a class (DirectAccess) how will you be able to
access it variable (Integer var ) in that class.

You can say that Buffalo is inaccessible inside USA even though the other parts of the
USA are accessible by saying that Buffalo is private .

similarly had your class DirectAccess been available to the Client , then
you can prevent them from accessing the Integer var by making that var private

You can however access a ref obj to the DirectAcces from the InDirectAccess because
the IndirectAccess is public and the obj var is not private / protected / default
but public.


*/

//DirectAccess da = new DirectAccess();
}
}


> //DirectAccess.java
> package mylib;

> class DirectAccess {
> public Integer var = new Integer(10);
> }

> ------------------------------------------------------

> //IndirectAccess.java
> package mylib;

> public class IndirectAccess {
> public DirectAccess obj= new DirectAccess();
> }

> ------------------------------------------------------

> //client.java
> import mylib.*;

> class client {
> public static void main(String arg[]) {
> IndirectAccess ia = new IndirectAccess();
> System.out.println(ia.obj.var);
> }
> }

> ------------------------------------------------------

> compile client.java, got complaint:

> client.java:6: The type mylib.DirectAccess to which the member var belongs is
> not accessible from class client.
> System.out.println(ia.obj.var);
> ^
> 1 error

>
> MY QUESTION IS:

> in class DirectAccess: wheather var is public or default doesn't make any difference, why wouldn't compiler
> complain about this, because if there is no way for any class outside mylib package to access "var", then
> its modifier "public" only cause confusion.

> Thanks,
> John






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us