aspose file tools*
The moose likes Beginning Java and the fly likes About  Scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About  Scope" Watch "About  Scope" New topic
Author

About Scope

JC Bismark
Greenhorn

Joined: Jun 27, 2008
Posts: 29
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

Joined: Feb 23, 2008
Posts: 89
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.


Karthick D V
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38840
    
  23
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

    Joined: Jun 27, 2008
    Posts: 29
    Thank you Campbell Ritchie, Karthick for your feedback.It is very helpful.
    Aditya Jha
    Ranch Hand

    Joined: Aug 25, 2003
    Posts: 227

    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

    Joined: Oct 13, 2005
    Posts: 38840
        
      23
    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

    Joined: Aug 25, 2003
    Posts: 227

    You're the boss!
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3542
        
      15
    Originally posted by Aditya Jha:
    You're the boss!


    No. He's just a hired hand
    Paul's the boss


    Joanne
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38840
        
      23
    Originally posted by Joanne Neal:


    No. He's just a hired hand
    Not even hired. We don't get paid anything here!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: About Scope