• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scope in method

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1) when I run this program it won't any compile error



error
-------
no comilation error

2)

class Test{
public static void main(String args[]) {
Test scopetest=new Test();
scopetest.passing(1);
}
public void passing(int forp) {
int i=0; // here it is giving error
for(int i=0;i<2;i++);

}
}

when declaration position is changed ,then it is giving the error.

why it is giving?

can you explain it?
can you provide me links on scope?

thanks in advance
ravikumar
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test{
public static void main(String args[]) {
Test scopetest=new Test();
scopetest.passing(1);
}
public void passing(int forp) {
int i=0; // here it is giving error
for(int i=0;i<2;i++);

}
}


Inside the passing method you are defining the variable i two times. First time you declare and init it at the commented line. Then you redefine and reinit in the loop.

Just replace the loop line of code with this one:
for(i=0; i<2; i++)

It shall work now...
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr Zaheer,you explanation ok. but I want How the compilre considers those
declarations. what is the specifications those declarations

thank you
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any variable declared has some scope after which it ceases to live and dies a painful death bestowed by the garbage collector.

So if you declare a variable in the for loop initialization block then it will live only within the for loop and once for() loop finishes to completeion it will not be available anymore.
for(int i=0;i<10 ;i++) { // i is avalable only in this block }
The whole point is where the variable has be declared if its declared within a method then its available only to the method.
If declared at class level and not marked static then its available to the entire class and each instance will have its own copy.
If declared at class level and marked static then again its available to the entire class but now will die only when the class is unloaded by JVM.

as for your question why was there a compiler error
void dummy() {
int i; // i will be declared in the dummy() stack and is available only to the method.
for(int i;i<10 ;i++) {} // When you do this you are trying to declare a new variable that you want to live only for the for() scope but with name i which is already declared in the dummy() stack and as you know you cannot have two variables with same name.
}

Hope this clears your confusion.
Thanks
Deepak
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Deepak Jain,

still I have a doubt.

I am to these conclusion. validate it.

1) for(int i=0;i<2;i++); int i=0;
//in this case after for loop excution ,i variable is dead, so I can
declare it again.
2) int i=1;
for (int i=1;i<2;i++) ;
// in this case , 'i' varibale scope is throught of the method and
again I am trying to declare variable 'i',so it gives the error.

3) can I declare the parameter of the method in the code blocks ?


thanks
ravikumar
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well for the first two , you are spot on.
Now as for

3) can I declare the parameter of the method in the code blocks ?



If this sentence meant to do something like this
void method(int a) {

int a; // Error because a is alread declared in method() stack
for (int a; a<1;a++); //Error again because its already declared in stack.
}
Hope this clears your doubt.
Thanks
Deepak
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you it created two variaveis of the same type and same name, in the same target, therefore the code does not compel. It imagines when you to want to call one the two created variaveis, which will be called? of it it will be? or to that you it created inside of it it will be? it perceives the problem?
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to all, I got it

with regards
ravikumar
 
reply
    Bookmark Topic Watch Topic
  • New Topic