473,322 Members | 1,736 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,322 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
9 1500
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
dmjpro
2,476 2GB
sorry it was my mistake .....


ok okkk again i am verryyy sorrryyyyy.........
Mar 13 '07 #7
RedSon
5,000 Expert 4TB
You're right r0. No need to copy this to c/c++
Mar 13 '07 #8
r035198x
13,262 8TB
sorry it was my mistake .....


ok okkk again i am verryyy sorrryyyyy.........

Hey DJ, you didn't do anything wrong. You were just asking and that's what the forum is for. We both learn that way.
Mar 14 '07 #9
dmjpro
2,476 2GB
ok thanxxxxx
Mar 14 '07 #10

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...
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...
5
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.