• 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

local varible scope

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Master
{
String doFileStuff() throws FileNotFoundException
{
return "a";
}
}

class Slave extends Master
{
public static void main(String [] args)
{
String s = null;
try
{
s= new Slave.doFileStuff();
}
catch(Exception e)
{ s= "b";}
System.out.println(s);
}
String doFileStuff()throws FileNotFoundException
{
return "b";
}
}

In the above pgm the stmt; s = new Slave().doFileStuff() ; assigns a value to s. And i think it is a local variable and its scope shud be only tot hat particular try block hows is taht i am gettin output as b. i think it shud "null".as s = b is limited to try block only and it is considerd as local variable.
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi saisnigdha samanchi

You have declared s into the main() method, which means it is local throughout the main(), not only a try block.
 
samanchi snigdha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mamun;
s there is local to main s in try block is local to try something called shadowing concept works here(right?) so i think it shud be giving null as answer. If at all printing of "s" is in try block itself it shud be giving an ouput as "b". According to my understanding..
 
samanchi snigdha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mamun;
s there is local to main s in try block is local to try something called shadowing concept works here(right?) so i think it shud be giving null as answer. If at all printing of "s" is in try block itself it shud be giving an ouput as "b". According to my understanding..
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first always write code in code tags. so that it will e easy to understand.
second variable "s" is declared outside try block so its not local to try block but it is local to main().


Se this
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saisnigdha samanchi:
Hi mamun;
s there is local to main s in try block is local to try something called shadowing concept works here(right?) so i think it shud be giving null as answer. If at all printing of "s" is in try block itself it shud be giving an ouput as "b". According to my understanding..




First of all please have a practice of pasting the actual-but-working code. In your code,

  • you have missed out the instantiation part "new Slave()" but its present in the next line when you explained.
  • Also the import statement for FileNotFoundException is missing.



  • I feel it would be a good practice, though it may not be really required as you may think.

    Do you really wanted to have the shadowing effect? If so, please read about shadowing here .

    Shadowing does NOT work within the arguments of same method.

  • Option a:

  • If at all you wanted to achieve this, you should have to DEFINE the variable "s" inside the method but you are sure to get the "variable already defined" exception during compilation itself.

  • Option b:

  • If you have declared the String variable "s" ONLY inside the try block, then you will get an exception in the System.out.println() statement as well with the message "cannot resolve symbol "s"."


    Hope this helps!
     
    Abdullah Mamun
    Ranch Hand
    Posts: 99
    Mac Eclipse IDE
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi saisnigdha samanchi

    I was a bit busy so cannot reply timely.
    But I hope, you already get the point after such a nice reply of Raghavan.

    Thanks Raghavan.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Al Mamun:


    Thanks Raghavan.



    My Pleasure
     
    samanchi snigdha
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanq Raghav and Mamun. got to know the concept =)
     
    Ranch Hand
    Posts: 202
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    please saisnigdha samanchi , syntax "
    reply
      Bookmark Topic Watch Topic
    • New Topic