Merge pull request #6110 from jlarroque/master

BAEL-2472 code examples
This commit is contained in:
Tom Hombergs 2019-01-28 21:16:46 +01:00 committed by GitHub
commit d86f9ed0c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 0 deletions

View File

@ -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++;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.variable.scope.examples;
import java.util.Arrays;
import java.util.List;
public class LoopScopeExample {
List<String> 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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}