Scope

From Wikibooks, open books for an open world
Jump to: navigation, search

Nested Classes Java Programming
Scope
Generics
Navigate Language Fundamentals topic: v  d  e )


[edit] Scope

The scope of a class, a variable or a method is its visibility and its accessibility. The visibility or accessibility means that you can use the item from a given place.

[edit] Scope of local variables

A local variable is visible after its declaration until the end of the block in which the local variable has been created.

Computer code
{
...
     // myNumber is NOT visible
  {
     // myNumber is NOT visible
     int myNumber;
     // myNumber is visible
    {
      ...
       // myNumber is visible
    }
       // myNumber is visible
  }
     // myNumber is NOT visible
...
}


[edit] Access modifiers

You surely would have noticed by now, the words public, protected and private at the beginning of class's method declarations used in this book. These keywords are called the access modifiers in the Java language syntax, and they define the scope of a given item.

[edit] For a class

  • If a class has public visibility, the class can be referenced by anywhere in the program.
  • If a class has package visibility, the class can be referenced only in the package where the class is defined.
  • If a class has private visibility, (it can happen only if the class is defined nested in an other class) the class can be accessed only in the outer class.

[edit] For a variable

  • If a variable is defined in a public class and it has public visibility, the variable can be referenced anywhere in the application through the class it is defined in.
  • If a variable has protected visibility, the variable can be referenced only in the sub-classes and in the same package through the class it is defined in.
  • If a variable has package visibility, the variable can be referenced only in the same package through the class it is defined in.
  • If a variable has private visibility, the variable can be accessed only in the class it is defined in.

[edit] For a method

  • If a method is defined in a public class and it has public visibility, the method can be called anywhere in the application through the class it is defined in.
  • If a method has protected visibility, the method can be called only in the sub-classes and in the same package through the class it is defined in.
  • If a method has package visibility, the method can be called only in the same package through the class it is defined in.
  • If a method has private visibility, the method can be called only in the class it is defined in.

[edit] For an interface

The interface methods and interfaces are always public. You do not need to specify the access modifier it will default to public. For clarity it is considered a good practice to put the public keyword.

The same way all member variables defined in the Interface by default will become static final once inherited in a class.

[edit] Summary

Class Nested class Method, or Member variable Interface Interface method signature
public visible from anywhere same as its class same as its class visible from anywhere visible from anywhere
protected N/A its class and its subclass its class and its subclass, and from its package N/A N/A
package only from its package only from its package only from its package N/A N/A
private N/A only from its class only from its class N/A N/A

The cases in blue are the default.

Clipboard

To do:
Add some exercises like the ones in Variables

Nested Classes Java Programming
Scope
Generics
Personal tools
Namespaces

Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export