473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

scope concept on java?????

dmjpro
2,476 2GB
i have code like this .. see carefully

void test
{
int i;
{
int i;
}
}
why does not java support this .... as C and C++ supports this

plz help me out .... thanxxxxx
Mar 13 '07 #1
5 1584
r035198x
13,262 8TB
i have code like this .. see carefully



why does not java support this .... as C and C++ supports this

plz help me out .... thanxxxxx
They are indistinguishable
What then would be the output of calling

Expand|Select|Wrap|Line Numbers
  1.  
  2. void test {
  3.  int i = 5;
  4.  {
  5.  int i = 10;
  6.  System.out.println(i + i);
  7.  }
  8. }
  9.  
?
Mar 13 '07 #2
dmjpro
2,476 2GB
i know it is not possible .....

in C and C++ there is a way to access the outer varibale using ::(scope resultion operator)

why this is stopped in java?????

thanxxxxxx
Mar 13 '07 #3
r035198x
13,262 8TB
i know it is not possible .....

in C and C++ there is a way to access the outer varibale using ::(scope resultion operator)

why this is stopped in java?????

thanxxxxxx
Even in c++ that wont work dj. You use the scope resolution operator when you have the context.

in
Expand|Select|Wrap|Line Numbers
  1.  void test { 
  2.  int i;
  3. }
  4.  
the context of i is the function test.
in
Expand|Select|Wrap|Line Numbers
  1.  void test{ 
  2.  int i;
  3. {
  4. int y;
  5. }
  6. }
  7.  
The contect of y is still the function test. The {} cannot be named and so you cannot use :: with it. The scope of y is still the function test.
so having
Expand|Select|Wrap|Line Numbers
  1.  void test{ 
  2.  int i;
  3. {
  4. int i;
  5. }
  6. }
  7.  
wont work because both have the same context i.e the function test and so you have a duplicate variable name
Mar 13 '07 #4
dmjpro
2,476 2GB
can't i access in C++ using TC++-3.0


void test()
{
int i;
{
printf("%d",::i); //outer i
printf("%d",i); //inner i
}
}

m i right ..... ??????

thanx
Mar 13 '07 #5
r035198x
13,262 8TB
can't i access in C++ using TC++-3.0


void test()
{
int i;
{
printf("%d",::i); //outer i
printf("%d",i); //inner i
}
}

m i right ..... ??????

thanx
I thought :: was for global namespace. Anyway that does not compile on my VS6.0 and you missed the declaration for the inner i that you had earlier.



P.S maybe I should copy this to the C++ forum.
Mar 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
5
by: elzacho | last post by:
I would like to (and commonly do) define my variables in the most specific scope I can. For example... int foo(int a, int b, int c) { /* don't declare temp here if we can help it */ ... ...
10
by: John Salerno | last post by:
Here's a sentence from Learning Python: "Names not assigned a value in the function definition are assumed to be enclosing scope locals (in an enclosing def), globals (in the enclosing module's...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
9
dmjpro
by: dmjpro | last post by:
i have code like this .. see carefully why does not java support this .... as C and C++ supports this plz help me out .... thanxxxxx
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
3
by: psroga | last post by:
Can anyone look at this code and let me know why pthread_mutex_unlock and pthread_mutex_lock are giving me the "phtread_mutex_unlock" was not defined in this scope error. I am compiling the code...
0
by: David Troxell - Encourager Software | last post by:
Product Scope 7 (http://www.encouragersoftware.com/) and Profile Exchanges enhanced with SetupCast publishing methods NOTE: If you are a software author, releasing commercial and/or shareware...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.