
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the scope of local variables in Java?
Scope of a variable denotes span of a variable.
The scope of a local variable is within that method i.e. when we create a variable with in a method, it cannot be accessed outside that method.
Example
If you observe the following example here, we have created a variable named num in the main method and, trying to access it in another method (demo).
public class SampleTest { String str = "sampleString"; public static void main(String args[]){ int num = 334; } public void demo(){ System.out.println(num); } }
Error
Since we cannot access a local variable (variable of a method) outside it, the compiler generates an error as −
C:\Sample>javac SampleTest.java SampleTest.java:7: error: cannot find symbol System.out.println(num); ^ symbol: variable num location: class SampleTest 1 error
- Related Questions & Answers
- What are Local Scope Variables in Postman?
- What is the scope of variables in JavaScript
- Local variables in Java
- Scope and lifetime of variables in Java?
- What are class variables, instance variables and local variables in Java?
- C++ Scope of Variables
- Member variables vs Local variables in Java
- Scope of Variables in C++
- Scope of Variables in C#
- Scope of Variables in Python
- What is a structure at local scope in C language?
- Global and Local Variables in Java
- What are local variables in C++?
- What is the difference between global and local variables in Python?
- What is the difference between global and local Variables in JavaScript?
Advertisements