Solved

Java: Scopping rules

Posted on 2009-05-15
7
203 Views
Last Modified: 2013-12-29
This is a question about scopping rules.   In the code sample I have purposfully done somthing goofy to as a test.  I expected that line 3, {int b = 3; int c = 4;} ,would result in redefinition of b and c and shadow the variables with the same names in line 2.  Instead it results in the compiler errors shown below.  My question is why is it that the inner scope of line 3 does not allow the variables in line 2 to be redefined?
------------------------------
src\Initializer.java:5: b is already defined in main(java.lang.String[])
                { int b = 3; int c = 4;}
                      ^
src\Initializer.java:5: c is already defined in main(java.lang.String[])
                { int b = 3; int c = 4;}
----------------------------
                                 ^
class Initializer{
 

	public static void main(String [] args){

		int b = 1, c = 2;

		{ int b = 3; int c = 4;}

		System.out.println("Initializer: " + b + " " + c);

	

	}

}

Open in new window

0
Comment
Question by:ssackett
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:emce
ID: 24399481
the {...} by itself doesn't produce a new visibility scope
0
 
LVL 7

Expert Comment

by:zwei
ID: 24399598
emce is right.

For what you want to do you'd have to do something like this:
class Initializer{

        static int b = 1;

        static int c = 2;
 

        public static void main(String [] args){

                int b = 3;

                int c = 4;

                System.out.println("Initializer: " + b + " " + c);

        

        }

}

Open in new window

0
 

Author Comment

by:ssackett
ID: 24399788
emce & Zwei,

Why does the attached code fail to compile?  It seems that the b and c declared inside the initializer can't be seen outside the block (what I would expect) but that the block doesn't behave like a nested scope in other ways.
...

  {int b = 2;}

  b = 1

...

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:Thomas4019
ID: 24399921
The point of a custom block decaration { }, is so that variables declared inside can be garbage collected and deleted outside of the block
0
 
LVL 7

Expert Comment

by:zwei
ID: 24400021
The variables are both local variables and are both on the same memory stack within a single method. When you create a new scope, you move on top of the original stack, which is why you can't create a new variable with the same name:
int a = 2;
{int a = 3;} <-- illegal, a is still on the stack.

But when you create a variable within a new scope, when you leave that scope the variable dies which is why this works:

{int b = 2;} <-- b dies here
int  b = 1;
0
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 125 total points
ID: 24400062
According to this link,

http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html

Java throws a compile time error because such naming can lead to very obscure and hard to detect errors.
0
 

Author Closing Comment

by:ssackett
ID: 31582049
Thanks for the help guys.  Yet another reason why it's dangerous to mix C and Java.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 81
advertisement module in core php 4 148
tomcat not starting 6 32
eclipse apache tomcat admin console 52 73
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now