BAEL-2472 code examples
This commit is contained in:
		
							parent
							
								
									3da571a218
								
							
						
					
					
						commit
						065a6e2519
					
				| @ -0,0 +1,15 @@ | ||||
| package org.baeldung.variable.scope.examples; | ||||
| 
 | ||||
| public class ClassScopeExample { | ||||
| 
 | ||||
|     Integer amount = 0; | ||||
| 
 | ||||
|     public void exampleMethod() { | ||||
|         amount++; | ||||
|     } | ||||
| 
 | ||||
|     public void anotherExampleMethod() { | ||||
|         amount--; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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; | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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"); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user