CodeGym /Courses /New Java Syntax /Visibility of variables

Visibility of variables

New Java Syntax
Level 3 , Lesson 3
Available

"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)
                          ┗━━━━━━━┛
  {
     System.out.println("Hi");
     String s = "Hi!";
   ┏┗━━━━┛
    System.out.println(s);
    if (args != NULL)
    {
       String s2 = s;
      ┗━━━━┛    
      System.out.println(s2);
     
    }
    Variables variables = new Variables();
    System.out.println(variables.instanceVariable);
    System.out.println(TEXT);
   
  }
 
   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
Comments (103)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
LaikaLaikaka Level 32, United States of America, United States
15 March 2025
when you say "anywhere in the program", what is a program? Do you mean a package? or a file?
Joshua Westbrook Level 6, United States of America, United States
11 July 2024
I'm color blind, is it possible to change the references to letters instead of colors? I have a hard time matching the description to the picture with colors
DEEMA Level 3, Saudi Arabia
30 July 2023
I don’t understand 😢
Anonymous #11274451 Level 3, United States of America, United States
5 August 2023
Public static void Private static void
Antonio5859 Level 3, Mexico
29 May 2023
Codegym puts everything that the books leave halfway but completely in a concise and accurate compendium. Spanish: Codegym pone todo lo que los libros dejan a medias pero de manera completa en un compedio conciso y acertivo. French: Codegym met tout ce que les livres laissent à moitié mais complètement dans un recueil concis et précis. Chinese: Codegym 將書籍中途留下的所有內容完整地放在簡明準確的綱要中。
Attila Ágoston Level 6, Hungary
29 March 2023
I do not understand this lesson. It is hard for me. Everyone is here, who speak Hungarian? Van itt valaki aki magyarul is el tudna magyarázni ezt a leckèt? Mert nekem ez a rèsz baromi érthetetlen. :(
Kelvin Level 14, Lagos, Nigeria
30 March 2023
would u like some help, i dont speak hungarian though but i could try explaining in English
Mark Varadi Level 3, Romania
11 January 2023
Why aren't you mentioning "protected" here also?
Lunita Level 4, Dominican Republic
15 December 2022
Where are the exercises for this lesson?!!
Manuel Macias Level 2, Spain
7 December 2022
Hola gente del tistoc
Juanma Level 2, Spain
9 June 2022
para cuando la traducción al español?
John Squirrels Level 41, San Francisco, Poland
10 June 2022
Unfortunately, we cannot provide any timeframes for translation.
Java Khan Level 3, Powder Springs, United States
19 May 2022
I would suggest to watch some youtube videos, it has more clear explanation. Sometimes it's hard to grasp the information from the first time, so try to digest slowly.