The Artima Developer Community
Sponsored Link

Java Answers Forum
visibility issue

5 replies on 1 page. Most recent reply: Aug 7, 2005 10:55 PM by rakesh kumar shukla

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
rakesh kumar shukla

Posts: 6
Nickname: rakshukla
Registered: Jul, 2005

visibility issue Posted: Aug 4, 2005 6:17 AM
Reply to this message Reply
Advertisement
please look at these classes
in <firstPackage.FirstClass> class static variable VAR_TO_TEST has visibility protected so if <secondPackage.SecondClass> class extends <firstPackage.FirstClass>, VAR_TO_TEST is visible. now as we can not assign less visibility to inherited variables so it should have protected visibilty in class <secondPackage.SecondClass> then why <secondPackage.CheckClass> is not able to get it ?
Whether I have to copy this variable into a instance static variable of secondPackage.SecondClass to make it available for secondPackage.CheckClass .
Please make me understand I am not good in OOP's funda


// FIRST CLASS
package firstPackage;

public class FirstClass {

protected static int VAR_TO_TEST = 0;

}
// FIRST CLASS ENDS HERE


// SECOND CLASS
package secondPackage;
import first.MyClass;

public class SecondClass extends FirstClass{

public static void main(String[] args) {
VAR_TO_TEST = 9;// VISIBLE HERE
}
}
// SECOND CLASS ENDS HERE

//CHECK CLASS
package secondPackage;
public class CheckClass {

public static void main(String[] args) {
SecondClass.VAR_TO_TEST = 9;// NOT VISIBLE HERE

}
}
// CHECK CLASS ENDS HERE


rakesh kumar shukla

Posts: 6
Nickname: rakshukla
Registered: Jul, 2005

Re: visibility issue Posted: Aug 4, 2005 6:25 AM
Reply to this message Reply
please take the code as

// FIRST CLASS
package firstPackage;

public class FirstClass {

protected static int VAR_TO_TEST = 0;

}
// FIRST CLASS ENDS HERE


// SECOND CLASS
package secondPackage;
import firstPackage.FirstClass;

public class SecondClass extends FirstClass{

public static void main(String[] args) {
VAR_TO_TEST = 9;// VISIBLE HERE
}
}
// SECOND CLASS ENDS HERE

//CHECK CLASS
package secondPackage;
public class CheckClass {

public static void main(String[] args) {
SecondClass.VAR_TO_TEST = 9;// NOT VISIBLE HERE

}
}
// CHECK CLASS ENDS HERE

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: visibility issue Posted: Aug 4, 2005 7:18 AM
Reply to this message Reply
> please take the code as
>
> // FIRST CLASS
> package firstPackage;
>
> public class FirstClass {
>
> protected static int VAR_TO_TEST = 0;
>
> }
> // FIRST CLASS ENDS HERE
>
>
> // SECOND CLASS
> package secondPackage;
> import firstPackage.FirstClass;
>
> public class SecondClass extends FirstClass{
>
> public static void main(String[] args) {
> VAR_TO_TEST = 9;// VISIBLE HERE
> }
> }
> // SECOND CLASS ENDS HERE
>
> //CHECK CLASS
> package secondPackage;
> public class CheckClass {
>
> public static void main(String[] args) {
> SecondClass.VAR_TO_TEST = 9;// NOT VISIBLE HERE
>
> }
> }
> // CHECK CLASS ENDS HERE

import static firstPackage.FirstClass.VAR_TO_TEST;

Though you'll have to make VAR_TO_TEST public
because they are from different packages.

Its not advisable to make copies of Static variables.
Take a look at Java's doc on static import:

http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

and *PLEASE* put pre tags around your code, its
really messy!! Check the Formatting your code
tip (it appears on your right when you're on the
post page).

Spike

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: visibility issue Posted: Aug 4, 2005 7:21 AM
Reply to this message Reply
Oh I forgot to mention the above only applies if you're
using Java 5.

Spike

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: visibility issue Posted: Aug 5, 2005 2:47 PM
Reply to this message Reply
The CheckClass has access only to the public variables and methods of SecondClass. Since the variable SecondClass.VAR_TO_TEST has "protected" access, it is visible only to sub-classes of SecondClass.

To grant visibility to any class, you can either define the variable to be public in FirstClass, or provide a public "getter" function in SecondClass (or even in FirstClass.)

rakesh kumar shukla

Posts: 6
Nickname: rakshukla
Registered: Jul, 2005

Re: visibility issue Posted: Aug 7, 2005 10:55 PM
Reply to this message Reply
But Ravi
I think protected members are accessible in the same package by other classes.

Flat View: This topic has 5 replies on 1 page
Topic: please help me on this question Previous Topic   Next Topic Topic: java.security.AccessControlException

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use