Scope class demonstrates field and local variable scopes : Class Fields « Class Definition « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. JSP
22. JSTL
23. Servlet
24. Web Services SOA
25. Email
26. J2ME
27. J2EE Application
28. XML
29. Design Pattern
30. Log
31. Security
32. Apache Common
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Class Definition » Class Fields 
5. 4. 1. Scope class demonstrates field and local variable scopes
public class MainClass
{
   public static void mainString args[] )
   {
      Scope testScope = new Scope();
      testScope.begin();
   }
}
class Scope
{
   private int x = 1

   public void begin()
   {
      int x = 5// method's local variable x shadows field x

      System.out.printf"local x in method begin is %d\n", x );

      useLocalVariable();
      useField();
      useLocalVariable();
      useField();

      System.out.printf"\nlocal x in method begin is %d\n", x );
   }

   public void useLocalVariable()
   {
      int x = 25// initialized each time useLocalVariable is called

      System.out.printf("\nlocal x on entering method useLocalVariable is %d\n", x );
      ++x; // modifies this method's local variable x
      System.out.printf
         "local x before exiting method useLocalVariable is %d\n", x );
   }

   // modify class Scope's field x during each call
   public void useField()
   {
      System.out.printf("\nfield x on entering method useField is %d\n", x );
      x *= 10// modifies class Scope's field x
      System.out.printf("field x before exiting method useField is %d\n", x );
   }
}
local x in method begin is 5

local x on entering method useLocalVariable is 25
local x before exiting method useLocalVariable is 26

field x on entering method useField is 1
field x before exiting method useField is 10

local x on entering method useLocalVariable is 25
local x before exiting method useLocalVariable is 26

field x on entering method useField is 10
field x before exiting method useField is 100

local x in method begin is 5
5. 4. Class Fields
5. 4. 1. Scope class demonstrates field and local variable scopes
5. 4. 2. Dynamically changing the behavior of an object via composition (the 'State' design pattern)
5. 4. 3. Scope for Class and Local Variables
5. 4. 4. A Class that Demonstrates Shadowing
w_w___w.__j__a__va___2__s_.c_o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.