0

So in java you write a public static variable and you have to access it outside of the class by saying class.var I find it strange that you can't access it by just by typing the variable name if your package is included in the main file.

Looking for an understanding of how the compiler works on java and object oriented languages that use this same class.publicstatic methodology.

  • See this [thread](https://stackoverflow.com/questions/2903166/meaning-of-the-import-statement-in-a-java-file), might help you understand what is going on. – Nexevis Jul 30 '21 at 17:20
  • @Andreas huh, don't know how I misread the post like that. There are static imports (which you've pointed out below), but yeah that would make my original comment irrelevant. – Rogue Jul 30 '21 at 18:48

2 Answers2

2

You can have static variables with the same name within different classes. It is possible that you will encounter an error in this request that you make without specifying a class name. to give an example ;

package com.axample;
import ...

public static class FooClass{
    public static int staticVariable;
    .
    .
    .  
}
package com.axample;
import ...

public static class BarClass{
    public static int staticVariable;
    .
    .
    .  ​
}


import static com.example.FooClass.*;
import static com.example.BarClass.*;

public class Main{
    public static void Main(String[] args){
        int baz = 0;
        //staticVariable = baz ;   //Error
        BarClass.staticVariable = baz;  //Must be
    } ​
}


0

Static members of a class are not in scope outside the class, not even from other classes in the same package.

You can however add them to the scope of your current class by adding a static import, e.g.

import static com.example.Foo.*;
import static com.example.Bar.doStuff;

That will add all static members of class Foo to the name scope.

It will also add all static overloads of method doStuff in the Bar class.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • OP asked about how the compiler works as well, so I'm gonna add: The compiler was designed that way because the Java designers wanted to be explicit about imports. There is no technical reason; it could have been implemented to automatically import static constants in the same package, but they chose not to to make the code more explicit, for example with these static imports. – Clashsoft Jul 30 '21 at 17:25
  • @Clashsoft There is also no *technical* reason why `import` is needed to gain unqualified access to types from other packages. Well, except for the fact that is violates name scoping rules, exactly like unqualified access to static members of another class would violate name scoping rules. In short, it would violate scoping rules, but you're right, that's not a "technical" reason; it could technically be done, with a lot of extra work by the compiler. --- As for how the compiler works: It works by following the *rules* as defined by the Java Language Specification. – Andreas Jul 30 '21 at 17:31