- 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
Global and Local Variables in Java
Local variables are declared in methods, constructors, or blocks. They are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
Access modifiers cannot be used for local variables. Local variables are visible only within the declared method, constructor, or block. These variables are implemented at stack level internally. Here, let us see an example of local variable −
Example
public class Demo { public void Rank() { int rank = 0; rank = rank + 7; System.out.println("Rank = " + rank); } public static void main(String args[]) { Demo d = new Demo(); d.Rank(); } }
Output
Rank = 7
There is no such concept of Global Variables in Java.
Advertisements