"Hello to my favorite student. Now I'm going to tell you about the visibility of variables."

"Huh? Can variables be invisible?"

"No. A variable's 'visibility', or scope, means the places in the code where you can refer to that variable. You may use some variables everywhere in the program, but others can only be used within their class, and still others – only within one method."

"For example, you can't use a variable before it has been declared."

"That makes sense."

"Here are a couple of examples:"

public class Variables

   private static String TEXT = "The end.";
   public static void main (String[] args)
     String s = "Hi!";
    if (args != NULL)
       String s2 = s;
    Variables variables = new Variables();
   public String instanceVariable;
   public Variables()
      instanceVariable = "Instance variable test.";

1. A variable declared in a method exists (is visible) from the start of its declaration to the end of the method.

2. A variable declared in a code block exists until the end of the code block.

3. A method's parameters exist everywhere within the method.

4. Variables in an object exist during the entire lifespan of the object that contains them. Their visibility is also defined by special access modifiers: public and private.

5. Static (class) variables exist the whole time the program is running. Their visibility is also defined by access modifiers.

"I love pictures. They help make everything clear."

"Good boy, Amigo. I always knew that you were a smart guy."

"I'm also going to tell you about 'access modifiers'. Don't be scared. There's nothing complicated about them. Here you can see the words public and private."

"I'm not scared. It's just that my eye is twitching."

"I believe you. You can manage how the methods and variables of one class are accessed by (or visible to) other classes. You can assign only one access modifier to each method or variable.

1. public access modifier.

You can use a variable, method or class marked with the public modifier from anywhere in the program. This is the highest level of access – there are no limitations here.

2. private access modifier.

You can use a variable or a method marked with the private modifier only from the class it is declared in. For all other classes, the marked method or variable will be invisible, just as if it doesn't exist. This is the highest level of closedness – access only within its own class.

3. No modifier.

If a variable or a method isn't marked with any modifier, it is considered to be marked with a 'default' access modifier. Such variables and methods are visible to all classes in the package they are declared in. And to them only. This level of access is sometimes called 'package-private' access, since access to the variables and methods is open for the entire package that contains their class.

Here's a table that summarizes what we've discussed:"

Modifiers Access from…
Own class Own package Any class
private Yes No No
No modifier (package-private) Yes Yes No
public Yes Yes Yes