diff --git a/core-java/src/main/java/com/baeldung/scope/BracketScopeExample.java b/core-java/src/main/java/com/baeldung/scope/BracketScopeExample.java new file mode 100644 index 0000000000..37ae211dcb --- /dev/null +++ b/core-java/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/src/main/java/com/baeldung/scope/ClassScopeExample.java b/core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java index 17ca1e01eb..241c6b466e 100644 --- a/core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java +++ b/core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java @@ -9,7 +9,6 @@ public class ClassScopeExample { } public void anotherExampleMethod() { - amount--; + Integer anotherAmount = amount + 4; } - -} +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java b/core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java index 2dd3be3333..8049a49c4d 100644 --- a/core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java +++ b/core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java @@ -12,7 +12,7 @@ public class LoopScopeExample { for (String name : listOfNames) { allNames = allNames + " " + name; } - + // compiler error, name cannot be resolved to a variable + // String lastNameUsed = name; } - -} +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java b/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java index 122d12e2fc..e62c6a2a1c 100644 --- a/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java +++ b/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java @@ -2,15 +2,12 @@ package org.baeldung.variable.scope.examples; public class MethodScopeExample { - Integer size = 2; - public void methodA() { Integer area = 2; - area = area + size; } public void methodB() { - size = size + 5; + // compiler error, area cannot be resolved to a variable + // area = area + 2; } - -} +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java b/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java index 9bd2268e52..fcd23e5ae0 100644 --- a/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java +++ b/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java @@ -9,5 +9,4 @@ public class NestedScopesExample { String title = "John Doe"; System.out.println(title); } - -} +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java b/core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java deleted file mode 100644 index 774095228c..0000000000 --- a/core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.baeldung.variable.scope.examples; - -public class OutOfScopeExample { - - public void methodWithAVariableDeclaredInside() { - String name = "John Doe"; - System.out.println(name); - } - - public void methodWithoutVariables() { - System.out.println("Pattrick"); - } - -}