Recent Changes - Search:

Java Tutorial

edit SideBar

VariableScope

These pages make up the course notes for my Java programming course (run at Dallam, Milnthorpe, Cumbria). Hopefully they also make a useful self-learning tutorial.

I probably should have mentioned this before now but I wanted to get you doing something real before getting bogged down in things like this. Although, that said, the concept is pretty easy. Variable Scope is the jargon used to describe from where a variable can be used. In other words, if you declare a variable in one place in your code, where else can you use it?

In a nutshell a varaible declared inside a code block (set of braces - curly brackets) is only visible within that block and any blocks nested inside it.

The following code illustrates this, guess what... create a new class and paste it in - I'll let you figure out the class name and package from the code itself, you should be getting the hang of this by now. Expect some errors, read the comments and try to figure why the errors are there

package tutorial.examples;

public class ScopeDemo {

        private static String classStatic = "i can be seen from anywhere in the CLASS";

        //a class level or variable
        private String classLevel = "i can be seen from anywhere in an OBJECT of this";

        private void aMethod() {
                //a variable in a method
                String methodLevel = "I can be seen in this method only";

                if (methodLevel.length() > 1 ) {
                        //a variable in a block
                        String outerBlock = "I can be seen anywhere in this block";

                        /* the variable "counter" is declared in the for
                        * statement and so can only be seen in the for block
                        */
                        for (int counter=0; counter<1; ++counter){
                                //what can be seen here
                                System.out.println( classStatic );
                                System.out.println( classLevel );
                                System.out.println( methodLevel );
                                System.out.println( outerBlock );
                                System.out.println( counter );
                        }

                        //what can be seen here
                        System.out.println( classStatic );
                        System.out.println( classLevel );
                        System.out.println( methodLevel );
                        System.out.println( outerBlock );
                        System.out.println( counter ); //not allowed because we are outside the for loop

                }
        }

        public static void main (String[] args) {
                //what can be seen here
                System.out.println( classStatic );
                System.out.println( classLevel ); //not allowed in "static" context
                System.out.println( methodLevel ); //not allowed - outside method
                System.out.println( outerBlock ); //not allowed - outside method and block
                System.out.println( counter ); //not allowed - outside method and block

                //make an object from this class - what can we see then
                ScopeDemo sd = new ScopeDemo();
                System.out.println( sd.classStatic );
                System.out.println( sd.classLevel );
                System.out.println( sd.methodLevel ); //not allowed - outside method
                System.out.println( sd.outerBlock );  //not allowed - outside method and block
                System.out.println( sd.counter );  //not allowed - outside method and block
        }

}

 

Creative Commons License

This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes
under the Creative Commons license Creative Commons Attribution-Noncommercial-Share Alike.

Edit - History - Print - Recent Changes - Search
Page last modified on May 18, 2007, at 02:44 PM