commit
						d86f9ed0c8
					
				| @ -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++; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user