Open In App

How to Check the Accessibility of the Static and Non-Static Variables by a Static Method?

Last Updated : 16 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The static keyword is a non – access modifier in Java which can be used for variables, methods, and block of code. Static variables in Java belong to the class i.e it is initialized only once at the start of the execution. By using static variables a single copy is shared among all the instances of the class, and they can be accessed directly by class name and don’t require any instance. The Static method similarly belongs to the class and not the instance and it can access only static variables but not non-static variables. 

Example 1: static methods can access static variables.

Java




// Java program to check accessibility
// of static variables inside
// static methods
 
class GFG {
 
    // declaring variable 'a' as static
    static int a = 5;
 
    // main is also a static type
    public static void main(String args[])
    {
 
        // accessing value of
        // static variable
        System.out.println("Static variable:" + a);
    }
}


 
 

Output

Static variable:5

 

Example 2: We cannot access non-static variables inside a static method.

 

Java




// Java program to check accessibility
// of non - static variables inside
// static methods
 
class GFG {
 
    // declaring variable 'a' as non - static
    int a = 5;
 
    // main is also a static type
    public static void main(String args[])
    {
 
        // accessing value of
        // non - static variable
        System.out.println("Non - Static variable:" + a);
    }
}


 
 

Output

 

prog.java:16: error: non-static variable a cannot be referenced from a static context
    System.out.println("Non - Static variable:" + a);
                                                  ^
1 error

 

Example 3: 

 

Java




// Java Program to demonstrate the accessibility
// of static and non-static variables
// by static method
 
class Student {
 
    // initialized to zero
    int a;
 
    // initialized to zero when class is loaded
    // but not for each object created.
    static int b;
 
    // Constructor
    Student()
    {
 
        // incrementing static variable b
        b++;
    }
 
    // method for printing
    public void printData()
    {
        System.out.println("Value of a = " + a);
        System.out.println("Value of b = " + b);
    }
 
    /*public static void increment(){
     a++;
    }*/
}
 
// Driver class
class GFG {
 
    // main is a static block
    public static void main(String args[])
    {
 
        // creating instance
        Student s1 = new Student();
 
        s1.printData();
 
        // directly accessing variable 'b'
        // by class name because it is static
        Student.b++;
 
        s1.printData();
    }
}


 
 

Output

Value of a = 0
Value of b = 1
Value of a = 0
Value of b = 2

 

In the above program if we remove comments of the increment method then an error will be raised because the increment method is static and variable a is non-static and we already know static methods cannot access non – static variables.

 



Similar Reads

Java Program to Check the Accessibility of an Static Variable By a Static Method
The Static Keyword is a non-access modifier in Java mainly used for memory management. This static keyword is mainly applicable for the following: VariablesMethodsBlocksNested Class Static Variable is a variable that is declared as static means a single copy of the variable is created and shared among all objects at the class level. Static method i
3 min read
Difference between static and non-static variables in Java
There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is declared as static, then a single copy of the vari
4 min read
Static and non static blank final variables in Java
A variable provides us with named storage that our programs can manipulate. There are two types of data variables in a class: Instance variables : Instance variables are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Instan
5 min read
Java Program to Check the Accessibility of an Instance variable by a Static Method
A static method belongs to the class rather than the object of a class. It can be invoked without the need for creating an instance of a class. It can access static data member and can change the value of it. The static keyword is a non – access modifier in Java which can be used for variables, methods, and block of code. Static variables in Java b
3 min read
Understanding storage of static methods and static variables in Java
In every programming language, memory is a vital resource and is also scarce in nature. Hence it’s essential that the memory is managed thoroughly without any leaks. Allocation and deallocation of memory is a critical task and requires a lot of care and consideration. In this article, we will understand the storage of static methods and static vari
4 min read
Difference between static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access any static method and static variable, without cr
6 min read
Why non-static variable cannot be referenced from a static method in Java
Java is one of the most popular and widely used programming language and platform. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In java, methods can be declared as either static or non-static. In this article, let's discuss why non-static vari
4 min read
Difference Between Static and Non Static Nested Class in Java
Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between making a class static or non-static. There are two kind
4 min read
How to Create an Accessibility Service in Android with Example?
Accessibility service is like a magic wand provided by the Android Framework. Although the sole purpose of the Accessibility service is to provide differently-abled an ease to access to their device, it can also be used to implement some functionalities that can help any user in his day-to-day phone usage. It is highly recommended that you should f
6 min read
Using Static Variables in Java
Here we will discuss the static variables in java. Java actually doesn’t have the concept of Global variable. To define a Global variable in java, the keyword static is used. The advantage of the static variable is discussed below. Now geeks you must be wondering out what are the advantages of static variable, why to incorporate in our program. Now
3 min read
Practice Tags :