- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to check the accessibility of an Instance Variable by a Static Method
The static methods are defined using static keywords but to declare instance variables we don’t use static keywords. Generally, we can’t access the instance variable by a static method.
In this article, we will create an instance variable and then we will check the accessibility of that instance variable by a static method. Let’s understand static method and instance variable first.
Instance Variable
In the context of programming language, variables are the name of containers that contain data of any type. We can say it is a storage unit.
Syntax to declare a variable
Data_Type nameOfvariable = values [optional];
A variable can be initialized at the time of declaration or we can initialize it when required in the whole program. But value must be of the same data type as specified at the time of declaration.
Instance
String str; // only declaration int price = 59; // declaration and initialization str = “Tutorix”; // initialization of ‘str’
Instance variables are one of the types of variables that are non-static. They are declared inside class but outside of every method scope of that class. They are accessible to all methods that belong to the class where it is defined. They can be public, private, protected, or default. If we don’t mention any access specifier then variable will be initialized with default access specifier.
Static Method
Static methods are defined using non access modifier static keyword. Normally, we call a method by creating an object of its class but static methods can be called without using objects. We call them using their class name only because static method belongs to class not to the instance of that class.
To call Static Mehtod
Class_name.static_method_name
For example, most of the members of inbuilt class Math is static. We can use them directly without creating its object.
Example 2
public class Main { public static void main( String[] args ) { double x = 6.55; double y = 4.32; System.out.println(" Ceil value of x: " + Math.ceil(x) ); System.out.println(" Floor value of y: " + Math.floor(y) ); System.out.println(" Value of PI = " + Math.PI); } }
Output
Ceil value of x: 7.0 Floor value of y: 4.0 Value of PI = 3.141592653589793
The above example illustrates the use of static methods ceil() and floor() of Math class. We can see that we have used them directly in our program without creating any object of Math class.
Checking Accessibility of an Instance Variable by Static Method
We already mentioned earlier in this article that we can’t access instance variables by a static method directly, we can only access them by creating an instance or object of the class.
Example
The following program demonstrates whether we can access instance variable by static method main() or not.
public class Main { public String str = "Tutorialspoint"; public static void main(String []args) { System.out.println(" Accessing instance variable " + str); } }
If we try to run the above code we will get an error.
Output
Main.java:4: error: non-static variable str cannot be referenced from a static context System.out.println(" Accessing instance variable " + str); ^ 1 error
Example
The following program illustrates how we can access an instance variable by a static method. We will create an object ‘obj’ of Main class and by using this object we can access the variable ‘str’.
public class Main { public String str = "Tutorialspoint"; public static void main(String []args) { Main obj = new Main(); // creating object using new keyword // To access the instance variable ‘str’ System.out.println(" Accessing instance variable: " + obj.str); } }
Output
Accessing instance variable: Tutorialspoint
Conclusion
In this article, we have understood the concept of instance variable and static method. Also, we discussed the accessibility of instance variable by a static method through the java programs.