Java MethodsLesson 23Free preview
Java Scope
This lesson is part of Java · All levels
Summary
Scope determines where a variable can be accessed in a program. Variables declared in a method can only be used in that method, whereas fields declared in a class level can be used by all methods in the class. Understanding the scope helps avoid missing variable errors or name conflicts.
Key points
- Local variables are declared in the method and only live in it
- Field classes are declared outside the method and are accessible by many methods
- The variable within the block {} only holds until the curly braces mark the closing
- The local variable name covers the shadow of the field name with the same name in the method
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.