简体   繁体   中英

How do I have some variables can be used in the whole package of Java?

I will need to have some variables, for example type Identifier, type Declaration, and both of these types are type Expression. And under the type Identifier, I want to have some variables, for example: ide, binder. They should have type Identifier, and under the type Declaration, I want to have: first, second, they should have type Declaration. What can I do to make it possible to refer to these variables later on without making an instance out of the class? Thank

So I first create a class Expression:

public abstract class Expression{   
}

And I have class Identifier:

public class Identifier extends Expression{
}

Finally I have class Declaration:

public class Declaration extends Expression{
}

The simple answer is to declare the variables 'static' and without 'public' or 'protected' or 'private' access modifiers, leaving the variables package-private or "default" access:

package com.yourdomain.whatever;
public class Identifier extends Expression {
  static Ide ide;
  static Binder binder;
  ...
}

The lack of access modifier means that the variables will be accessible to other types in the same package, but not outside the package.

But two things are bad about this idea: you are exposing variables directly, and you are using 'static' variables, which are essentially global variables and therefore quite nasty.

You could make the variables more widely accessible by declaring them 'public' or 'protected', but that just makes matters worse.

So I ask why you want those variables to be directly accessible, and what is your reason not to instantiate the class? Neither of those seem like great ideas from here.

One option would be to make these variables static and final , effectively making them constants:

public class Identifier extends Expression {
    public static final String ide = "IntelliJ";
    public static final String binder = "binder";

    // ...
}

And now these variables can be accessed directly using the class name:

String myIde = Identifier.ide;

To make a global variable, make it public and static . Making it public makes it so that the variable is accessible anywhere in the package. Making it static makes it so that you don't have to make an instance of the class to access the variable. For example,

public class Identifier extends Expression{
    public static int globalVar;
    //...
}

Then it can be accessed anywhere else in the package in other classes by calling Identifier.globalVar for instance.

EDIT: I should make note that the access identifier public makes it so that the variable is accessible anywhere in the Java program, whether it'd be inside or outside the package. Access identifier protected makes it accessible in the class, package, and subclasses. If you want the variable to be accessible in the same package only and not in subclasses or anywhere outside the package, then the proper choice is to have no access identifier. However, if it doesn't matter, then any of the three choices discussed will suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM