From 065a6e2519d5b52093381a388f9075c5580d8f7f Mon Sep 17 00:00:00 2001 From: jose Date: Tue, 8 Jan 2019 23:55:48 -0300 Subject: [PATCH] BAEL-2472 code examples --- .../com/baeldung/scope/ClassScopeExample.java | 15 +++++++++++++++ .../com/baeldung/scope/LoopScopeExample.java | 18 ++++++++++++++++++ .../com/baeldung/scope/MethodScopeExample.java | 16 ++++++++++++++++ .../baeldung/scope/NestedScopesExample.java | 13 +++++++++++++ .../com/baeldung/scope/OutOfScopeExample.java | 14 ++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java create mode 100644 core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java create mode 100644 core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java create mode 100644 core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java create mode 100644 core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java diff --git a/core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java b/core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java new file mode 100644 index 0000000000..17ca1e01eb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/scope/ClassScopeExample.java @@ -0,0 +1,15 @@ +package org.baeldung.variable.scope.examples; + +public class ClassScopeExample { + + Integer amount = 0; + + public void exampleMethod() { + amount++; + } + + public void anotherExampleMethod() { + amount--; + } + +} diff --git a/core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java b/core-java/src/main/java/com/baeldung/scope/LoopScopeExample.java new file mode 100644 index 0000000000..2dd3be3333 --- /dev/null +++ b/core-java/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; + } + + } + +} diff --git a/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java b/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java new file mode 100644 index 0000000000..122d12e2fc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/scope/MethodScopeExample.java @@ -0,0 +1,16 @@ +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; + } + +} diff --git a/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java b/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java new file mode 100644 index 0000000000..9bd2268e52 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/scope/NestedScopesExample.java @@ -0,0 +1,13 @@ +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); + } + +} diff --git a/core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java b/core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java new file mode 100644 index 0000000000..774095228c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/scope/OutOfScopeExample.java @@ -0,0 +1,14 @@ +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"); + } + +}