The scope of a variable

Proper programming languages have variables which can be in and out of scope. In other words: variables that can be seen by other routines and variables that cannot. In Modula-2 a global variable is accessible in the full program. Unless a procedure has a local variable with the same name. Then the local variable supersedes the global variable.
Java seems to be a proper language. So Java should have a scope of variables as well. Let's see if we can find the scope. Here's a short test program:

class scope
{
   double pi = 3.14;
   
   public static void main ( String args [] )
     {
	float pi = 22f/7f;
	System.out.println ("Internal pi = " + pi);
	System.out.println ("global pi   = " + .pi);
     }
}
   
I declared a variable 'pi' outside the main routine. This must be a global variable. I'm not sure yet how to access the variable, but the dot operator must be part of the game. See how this compiles...
jan@Beryllium:~/develop/java$ javac scope.java
scope.java:9: illegal start of expression
        System.out.println ("global pi   = " + .pi);
                                               ^
scope.java:9: ';' expected
        System.out.println ("global pi   = " + .pi);
                                                ^

scope.java:9: illegal start of expression
        System.out.println ("global pi   = " + .pi);
                                                  ^
3 errors
jan@Beryllium:~/develop/java$
   
Three errors in one short source. Apparently the naming of the variable is not OK. Lets change it a bit more:
class scope
{
   double pi = 3.14;
   
   public static void main ( String args [] )
     {
	float pi = 22f/7f;
	System.out.println ("Internal pi = " + pi);
	System.out.println ("global pi   = " + scope.pi);
     }
}
   
Run it through the compiler....
jan@Beryllium:~/develop/java$ javac scope.java
scope.java:9: non-static variable pi cannot be referenced from a static context
        System.out.println ("global pi   = " + scope.pi);
                                                    ^
1 error
jan@Beryllium:~/develop/java$
   
66% of the errors gone already. Not bad. Still, there's something with the staticness of the variable. Let's see how we're gonna tackle this opportunity (it's an opportunity [to learn}, not a problem):
class scope
{
   static double pi = 3.14;
   
   public static void main ( String args [] )
     {
	float pi = 22f/7f;
	System.out.println ("Internal pi = " + pi);
	System.out.println ("global pi   = " + scope.pi);
     }
}
   
The word in red was added. See how it compiles.
jan@Beryllium:~/develop/java$ javac scope.java
jan@Beryllium:~/develop/java$
   
No problemo! Hasta La vista amigo! No mountain is too high for a genius like me! See how it runs though...
jan@Beryllium:~/develop/java$ java scope
Internal pi = 3.142857
global pi   = 3.14
jan@Beryllium:~/develop/java$
   
Not too bad at all! It seems to work. Apparently global variables need to be static. See what happens if I make it public instead of static (I won't show the source here; you can understand what I mean):
jan@Beryllium:~/develop/java$ javac scope.java
scope.java:9: non-static variable pi cannot be referenced from a static context
        System.out.println ("global pi   = " + scope.pi);
	                                            ^
1 error
jan@Beryllium:~/develop/java$
   
Publicness is not enough, apparently. Staticness is. I'm gonna change it back into a static variable that's public as well. See how that runs:
jan@Beryllium:~/develop/java$ jed scope.java
jan@Beryllium:~/develop/java$ javac scope.java
jan@Beryllium:~/develop/java$ java scope
Internal pi = 3.142857
global pi   = 3.14
jan@Beryllium:~/develop/java$
   
After editing, it compiles cleanly and it runs like a charm. So the publicness is not important if you want to use a global variable. If I look at this, I get the impression Java was designed by a japanese. Japanese languages are either without any (obvious) underlying structure, or complete and utterly overstructured. Java tends to the latter. It's better than C++, but it won't even come close to Oberon or Modula-2.

here's the final source that compiles cleanly:
class scope
{
   static public double pi = 3.14;
   
   public static void main ( String args [] )
     {
	float pi = 22f/7f;
	System.out.println ("Internal pi = " + pi);
	System.out.println ("global pi   = " + scope.pi);
     }
}
   


Page created on 5 June 2010 and

Page equipped with FroogleBuster technology