- 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
Local variables in Java
- Local variables are declared in methods, constructors, or blocks.
- Local variables 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.
- Local variables are implemented at stack level internally.
- There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.
Example
Here, age is a local variable. This is defined inside pupAge()method and its scope is limited to only this method.
public class Test { public void pupAge() { int age = 0; age = age + 7; System.out.println("Puppy age is : " + age); } public static void main(String args[]) { Test test = new Test(); test.pupAge(); } }
Output
Puppy age is: 7
- Related Articles
- Member variables vs Local variables in Java
- Global and Local Variables in Java
- What are class variables, instance variables and local variables in Java?
- Do local variables in Java have default values?
- Are static local variables allowed in Java?\n
- Can a method local inner class access the local final variables in Java?
- What is the scope of local variables in Java?
- System variables vs Local Variables in MySQL?
- User-defined variables vs Local Variables in MySQL?
- Final local variables in C#
- Can access modifiers be used for local variables in Java?
- What are local variables and global variables in C++?
- Global and Local Variables in Python?
- Global vs Local variables in Python
- What are local variables in C++?

Advertisements