Skip to content

Commit d86f9ed

Browse files
authored
Merge pull request eugenp#6110 from jlarroque/master
BAEL-2472 code examples
2 parents 35f38c8 + ca5ff8a commit d86f9ed

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.variable.scope.examples;
2+
3+
public class BracketScopeExample {
4+
5+
public void mathOperationExample() {
6+
Integer sum = 0;
7+
{
8+
Integer number = 2;
9+
sum = sum + number;
10+
}
11+
// compiler error, number cannot be solved as a variable
12+
// number++;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.variable.scope.examples;
2+
3+
public class ClassScopeExample {
4+
5+
Integer amount = 0;
6+
7+
public void exampleMethod() {
8+
amount++;
9+
}
10+
11+
public void anotherExampleMethod() {
12+
Integer anotherAmount = amount + 4;
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.variable.scope.examples;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class LoopScopeExample {
7+
8+
List<String> listOfNames = Arrays.asList("Joe", "Susan", "Pattrick");
9+
10+
public void iterationOfNames() {
11+
String allNames = "";
12+
for (String name : listOfNames) {
13+
allNames = allNames + " " + name;
14+
}
15+
// compiler error, name cannot be resolved to a variable
16+
// String lastNameUsed = name;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.variable.scope.examples;
2+
3+
public class MethodScopeExample {
4+
5+
public void methodA() {
6+
Integer area = 2;
7+
}
8+
9+
public void methodB() {
10+
// compiler error, area cannot be resolved to a variable
11+
// area = area + 2;
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung.variable.scope.examples;
2+
3+
public class NestedScopesExample {
4+
5+
String title = "Baeldung";
6+
7+
public void printTitle() {
8+
System.out.println(title);
9+
String title = "John Doe";
10+
System.out.println(title);
11+
}
12+
}

0 commit comments

Comments
 (0)