Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: What does SCOPE actually mean in java language?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default What does SCOPE actually mean in java language?

    So what does SCOPE actually mean in java language?

    is it like who can access information or what exactually is it?

    Can someone please give me more information on the concept of SCOPE.

    Thanks very much.

    EDIT

    I came up with this:

    The basic meaning of scope is “who can use or access the variables”. Java class variables have six possible levels of visibility to other classes. The six visibility levels are published, public, protected, default, private and local. Each scope has a different visibility and purpose.

    Correct me if im wrong thanks.

    So from this piece of code:

     1 public class Scope {
    2 public static void test(int num) {
    3 if (num > 10) {
    4 int half = num / 2;
    5 System.out.println(“Half num = “ + half);
    6 } else {
    7 System.out.println(num);
    8 }
    9 }
    10 public static void main(String[] args)
    11 {
    12 int x = 5;
    13 test(x);
    14 }
    15 }

    What is the scope of the specified variables? Specifying the line
    numbers where they are visible.

    Answer: Scope: Public: Visible to all classes and is it line 1 2 and 5?

    Quite new to this stuff, still learning. Thanks for your help.
    Last edited by wAdeski; November 30th, 2010 at 09:02 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What does SCOPE actually mean in java language?

    In a simple sense, and the more typical context I've seen the word used, scope refers the duration and context of which a variable is accessible. Create a variable in a method, and unless returned by the method the variable looses scope when the method exits. Create a variable within a block of code, and its only accessible within that block. Create a variable in a method, and call another method trying to reference said variable without passing it as a parameter it is out of scope.
    public void method1(){
        int x = 0;
        method();
    }
    public void method(){
        //x is out of scope
        for ( int i = 0; i < 10; i++ ){
           int val = 20;
            //do something with i
        }
        //right now, i is out of scope
        //right now, val is out of scope
    }
    And according to my vague definition above ('the duration and context of which a variable is accessible' - so vague it's debatable), access modifiers are another form of varying scope. The list you itemize goes over the what seems like the basic definitions of variable scope, but there are a few other more complex scopes that aren't defined in that list (one example - server/client communications), which result in runtime rather than compile time errors (one could probably debate whether they are technically scope problems - in my opinion they are but in a different sort of context).
    Last edited by copeg; December 1st, 2010 at 12:08 AM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    wAdeski (December 2nd, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What does SCOPE actually mean in java language?

    thanks very much!

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What does SCOPE actually mean in java language?

    I guess the last thing to add is that scope != lifetime of the variable.

    Lifetime deals with when a particular piece of data exists in memory. A simple example would be to have two variables point to the same object, and let one variable goes out of scope. The object will still exist in memory and can be accessed because it's still "alive".

    Even when a variable goes out of scope, the data does not cease to exist immediately (ideally it would be, but that would take too much resources). It's alive until it's released from memory (in Java this is done automatically by the Garbage Collector). An analogy is being trapped (but alive) on a desert island vs. being dead.

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    wAdeski (December 2nd, 2010)

Similar Threads

  1. Natural language processing in java
    By nikki. in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 11:34 PM
  2. The simpler the program - seemingly the more scope for error :p
    By Bacon n' Logic in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 14th, 2010, 06:10 PM
  3. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  4. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM
  5. Replies: 5
    Last Post: September 6th, 2009, 04:39 AM

Tags for this Thread