
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Global and Local Variables in C#
- Global and Local Variables in Python?
- What are local variables and global variables in C++?
- Global vs Local variables in Python
- What is the difference between global and local variables in Python?
- What is the difference between global and local Variables in JavaScript?
- What are the rules for local and global variables in Python?
- How are C++ Local and Global variables initialized by default?
- Global variables in Java
- Local variables in Java
- What are class variables, instance variables and local variables in Java?
- Member variables vs Local variables in Java
- Global and Local Inversions in C++
- Difference Between Local and Global Variable
- Do local variables in Java have default values?

Advertisements