-
final variables in method
Hi,
A method can have final and non-final variables in java.
1.Where these(final&non-final) variables are stored in JVM?
2.what is the life time of final variables of a method?
3.Can we access final variables of a method out side of that method, if so how? Give me example and detailed explanation.
Wating for your answers....
-
final variables are replaced with the actual value wherever it is used during compilation itself.
eg:
1. final int a=20,b=30;
2. int c ;
3. c = a + b;
In the third line, since a & b are final variables, the compiler will replace this statement as
c = 20 + 30;
So, since this is part of method definition, it doesn't require separate memory location. (This will be stored in method area as part of method definition).
This can lead to a bug.
See the following scenario..
If we have 2 different classes in two source file, and the first one is using a final variable of second class, and if you change the value of final variable of the second class, and if we didn't recompile the first class, still the first class will hold the previous value.
Non final variables have local scope and it stores in stack of JVM.
2. All local variables have life time only inside the block where it defines (including final).
3. No local variables (including final) can access out side the block where it is defined.
Similar Threads
-
Replies: 0
Last Post: 12-16-2006, 06:06 PM
-
Replies: 5
Last Post: 05-20-2002, 12:35 PM
-
By Patrick Ireland in forum .NET
Replies: 5
Last Post: 05-10-2001, 06:19 PM
-
By Patrick Ireland in forum .NET
Replies: 0
Last Post: 04-26-2001, 10:01 PM
-
Replies: 3
Last Post: 04-13-2001, 09:13 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks