diff --git a/core-java-lang/src/main/java/com/baeldung/scope/BracketScopeExample.java b/core-java-lang/src/main/java/com/baeldung/scope/BracketScopeExample.java new file mode 100644 index 0000000000..37ae211dcb --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/scope/BracketScopeExample.java @@ -0,0 +1,14 @@ +package org.baeldung.variable.scope.examples; + +public class BracketScopeExample { + + public void mathOperationExample() { + Integer sum = 0; + { + Integer number = 2; + sum = sum + number; + } + // compiler error, number cannot be solved as a variable + // number++; + } +} \ No newline at end of file diff --git a/core-java-lang/src/main/java/com/baeldung/scope/ClassScopeExample.java b/core-java-lang/src/main/java/com/baeldung/scope/ClassScopeExample.java new file mode 100644 index 0000000000..241c6b466e --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/scope/ClassScopeExample.java @@ -0,0 +1,14 @@ +package org.baeldung.variable.scope.examples; + +public class ClassScopeExample { + + Integer amount = 0; + + public void exampleMethod() { + amount++; + } + + public void anotherExampleMethod() { + Integer anotherAmount = amount + 4; + } +} \ No newline at end of file diff --git a/core-java-lang/src/main/java/com/baeldung/scope/LoopScopeExample.java b/core-java-lang/src/main/java/com/baeldung/scope/LoopScopeExample.java new file mode 100644 index 0000000000..7bf92a6d26 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/scope/LoopScopeExample.java @@ -0,0 +1,18 @@ +package org.baeldung.variable.scope.examples; + +import java.util.Arrays; +import java.util.List; + +public class LoopScopeExample { + + List listOfNames = Arrays.asList("Joe", "Susan", "Pattrick"); + + public void iterationOfNames() { + String allNames = ""; + for (String name : listOfNames) { + allNames = allNames + " " + name; + } + // compiler error, name cannot be resolved to a variable + // String lastNameUsed = name; + } +} diff --git a/core-java-lang/src/main/java/com/baeldung/scope/MethodScopeExample.java b/core-java-lang/src/main/java/com/baeldung/scope/MethodScopeExample.java new file mode 100644 index 0000000000..e62c6a2a1c --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/scope/MethodScopeExample.java @@ -0,0 +1,13 @@ +package org.baeldung.variable.scope.examples; + +public class MethodScopeExample { + + public void methodA() { + Integer area = 2; + } + + public void methodB() { + // compiler error, area cannot be resolved to a variable + // area = area + 2; + } +} \ No newline at end of file diff --git a/core-java-lang/src/main/java/com/baeldung/scope/NestedScopesExample.java b/core-java-lang/src/main/java/com/baeldung/scope/NestedScopesExample.java new file mode 100644 index 0000000000..fcd23e5ae0 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/scope/NestedScopesExample.java @@ -0,0 +1,12 @@ +package org.baeldung.variable.scope.examples; + +public class NestedScopesExample { + + String title = "Baeldung"; + + public void printTitle() { + System.out.println(title); + String title = "John Doe"; + System.out.println(title); + } +} \ No newline at end of file