0

Hey guys I am a total java newbie and to be honest I am not sure on how to explain my problem to you.

So I have 2 classes, one of which is inherited by the other.

Now I am not allowed to change the superclass, or else this would be much easier.

The problem is that in my subclass I want to access a variable (let's call it variable1)

This variable is defined like following in the superclass:

static final String variable1 = (String)AccessController.doPrivileged(new PrivilegedAction()
 {
    public Object run() {
        return System.getProperty("variable1", "\n");
    }
 }
);

Now in my subclass I try the following:

this.finalOutputFormat = (replaceKeys(this.format) + variable1);

But it doesn't work because eclipse keeps telling me that variable1 "is not visible".

That is pretty much the only error I've got.

Do you have any idea why variable1 is not visible to my subclass? The superclass is imported as a library but in a different package, obviously.

I hope my description of the situation is not too confusing but right now I don't have much more information than this.

Thanks.

Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27
  • 1
    http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – arshajii Aug 05 '13 at 13:56
  • 1
    If you google "java visibility" you will find (probably in first result) this [official doc](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html). If you read it you will notice that variables with "no visibility modifier" can be accessed only in the same class, or in the same package. – Pshemo Aug 05 '13 at 13:59
  • Please mark the response as the answer. – Deactivator2 Aug 05 '13 at 15:05

2 Answers2

4

There is no visibility keyword (public, private, protected) on variable1. Hence, it is only accessible from classes which are in the same package.

If you are not allowed to modify your superclass, your only option is to put your class in the same package as the superclass.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • 1
    The "_and subclasses_" part of the answer seems to be wrong. See the links in the comments below the question. – jlordo Aug 05 '13 at 14:03
  • There's also the intermediate option of having a "Proxy" class in the superclass package, containing only code for exposing variable1, and the subclass merely uses the proxy. This way, the bulk of the subclass logic isn't forced to change packages. – C.B. Sep 25 '13 at 22:41
0

if you want to use it like this, put a public modifier before variable1 or move sub class to the same package as super class

charles_ma
  • 786
  • 7
  • 10