Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Scope

 
JC Bismark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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
 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • 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
    • Quote
    • Report post to moderator
    Thank you Campbell Ritchie, Karthick for your feedback.It is very helpful.
     
    Aditya Jha
    Ranch Hand
    Posts: 227
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • 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
    Sheriff
    Posts: 48394
    56
    • Mark post as helpful
    • send pies
    • 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 Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're the boss!
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • 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
    Sheriff
    Posts: 48394
    56
    • Mark post as helpful
    • send pies
    • 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!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic