• 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

About Scope

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's the best way to answer the question:" what is the scope?"

I know that names are used to identify entities declared in the program like classes,method,variables,parameter,constructos,etc.Every declaration has a scope that is different.
Do I need to specify the areas of a program where each declaration is contained or take only one example ? please advise,thanks
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Not sure if this is the right explanation that you can give.

We can tell that as the "VISIBILITY OF THAT PARTICULAR VARIABLE/METHOD"

Hope we can also tell an example. Say you have an private variable[var1] sitting in a class1. That particular variable is visible only for that class1. For the other classes they wont even know that class1 has a variable named as var1.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch . I have a very simple and crude and doubtless inaccurate rule of thumb for scope.

Local Variables:
You know you divide blocks up with {} pairs? Well,
  • Find the last { which precedes where you declare the variable.
  • Find the } which matches it
  • Your scope is from where you declare the variable until that }
  • Fields and Parameters:
  • A field is "in scope" for the whole of the class. Imagine your fields start from the { which begins the class.
  • A parameter (in () after the method name) doesn't have a preceding {, so take the nearest { which is the beginning of the method, so it is in scope for the whole method.
  •  
    JC Bismark
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Campbell Ritchie, Karthick for your feedback.It is very helpful.
     
    Ranch Hand
    Posts: 227
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Campbell,

    Just a suggestion...

    You might concise your definition for Local Variables' Scope by one bullet point:

  • Find the next } from the place where you declare the variable
  • Your scope is from where you declare the variable until that }


  • Best regards,

    - Aditya Jha

    [ June 29, 2008: Message edited by: Aditya Jha ]
    [ June 29, 2008: Message edited by: Aditya Jha ]
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Aditya Jha:
    Hi Campbell,

    Just a suggestion...

    You might concise your definition for Local Variables' Scope by one bullet point:

  • Find the next } from the place where you declare the variable
  • Your scope is from where you declare the variable until that }


  • Best regards,

    - Aditya Jha

    Thank you, but the scope of a local variable is not until the next }. You mean the matching }, not the next }.

    Look at this silly method
     
    Aditya Jha
    Ranch Hand
    Posts: 227
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're the boss!
     
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Aditya Jha:
    You're the boss!



    No. He's just a hired hand
    Paul's the boss
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Joanne Neal:


    No. He's just a hired hand

    Not even hired. We don't get paid anything here!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic