• Post Reply Bookmark Topic Watch Topic
  • New Topic

scope  RSS feed

 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dot separator and flow of code in this example challenges my understanding.
I would have thought the outcome would have been 14 at the very least due to scope.var = var + 2 in line 6
Why do lines 6 and 7 not alter the variable var?
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

That is because, your local variable var is never modified. Please note that even when you say 'var + 2', it does not change the value of var (unless you say 'var = var + 2').

Does this answer your question?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are incorrectly assuming that the int var that you declared outside the method and the one you declared inside it are the same because they have the same identifier. That is not true. Any member variable that is declared outside a method and inside a class is what we call as an instance variable or the field of a class. The one you have inside the method is called as a local variable. Even if they have the same identifier, they are two completely different entities. Method local variables live on the method stack area when the method gets loaded until it finishes execution and the stack gets dissolved. On the other hand, the scope of an instance variable or a field is throughout the class. Also, every instance of a class will have it's own copy of the instance variable var and will reside on the area of the memory called the heap.



Read this tutorial and try and understand each type properly.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 3 distinct and completely independent "var" variables: The member variable, the local in main, and the local (parameter) in adjustVar. Changing one of them has no effect on the others.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Please note that even when you say 'var + 2', it does not change the value of var (unless you say 'var = var + 2').


He's doing var += 2; which changes the value of var. It's just a different var from the one he's printing out.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Any member variable that is declared outside a method and inside a class is what we call as an instance variable or the filed of a class.


Unless it's static. Then it's a class varaible.

 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does not yet make complete understanding.

If var were different, naming them differently should have no effect. Correct?

Another thing I noticed is that var seems to be relating to itself as the program progresses:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Starr wrote:If var were different, naming them differently should have no effect. Correct?


They are 3 completely different variables, and changing all their names will have no effect, because you'll have exactly the same program.

Here's your original code:



Now here's the exact same code, producing the exact same behavior, with the 3 variables renamed:


Another thing I noticed is that var seems to be relating to itself as the program progresses:


No idea what you mean here.
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant by that is it looked like var was being affected as the sequence of the program progressed.

This makes more sense, as these variables are explicitly labeled. Thank you. To have the same name for three different occurrences is confusing and seems like a bad practice to me. Some of this is not very straightforward. Unlike math, to me. I desire to learn. However, The more I look at this stuff the more I seem to get confused. It seems the conditions have conditions and those conditions have nested conditions within them. Will I ever get this stuff? I seem to understand the text, then when I look at snippets of code, I prove that I am thoroughly confused, and time seems wasted.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Starr wrote:What I meant by that is it looked like var was being affected as the sequence of the program progressed.


You have 3 variables named var. You did things like "var =" and "var +=", so, yes, at least one of your 3 var variables was being affected.

This makes more sense, as these variables are explicitly labeled. Thank you.


You're welcome. I'm glad it's clearer now.

To have the same name for three different occurrences is confusing and seems like a bad practice to me.


Not really. The rules are quite simple, except possibly for some corner cases.

  • The scope of a local is the nearest enclosing block in which it's declared (often just the method, but may be a loop, if, etc.), so the locals from any one method are always completely separate from the locals for any other method. (And note that parameters count as locals.)
  • The scope of a static member variable is the entire class.
  • The scope of a non-static member variable is constructors, methods, and instance initializers for that class, with each instance getting its own copy.
  • In the case where both a local and member are in scope, the local gets precedence. If we want to refer to the member, we have to precede it with this. for non-static members and <class_name>. for static members (where <class_name> would be replaced by the actual name of the class.


  • That covers the vast majority of the cases you'll encounter, and understanding it when reading code is easier than understanding those few bullet points.

    Stuff like this is quite common, and with a little practice, it's not confusing at all:

     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you. I will need to be aware of what the scope is right along with noticing the name of the variable itself. I keep hearing practice and you will get it. So, I keep practicing, looking, typing, reading, etc.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    David Starr wrote:I keep hearing practice and you will get it. So, I keep practicing, looking, typing, reading, etc.


    Make sure that practice involves more than just going through the motions. Typing out a bunch of classes will give you muscle memory for braces and semicolons, but you have to stop, think, and study when you hit something you don't understand. And when stuff does work as you understand, look at each piece and make sure you know what it does and why it works. Test your assumptions. Change something that you've been taking for granted and predict what will happen as a result.

    It has to be mentally active, engaged practice to be effective.
     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting, and helpful.

    You noticed something I may have been doing in order to simply gain exposure in hopes it would help to put the overall puzzle together.

    I pressure myself to gain this understanding, and had not considered the approach of seeking to understand WHEN something DOES run smoothly, why it did so.
     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Say, after a bit of a break it turns out I am stuck on this again. Code example:


    Produces result:

    C:\src>java VarScope1
    var0 = 12
    var1 = 10
    var3 = 12
    var4 = 14
    var2 = 10


    How come this is not the print order?:
    var0 = 12
    var1 = 10
    var2 = 10
    var3 = 12
    var4 = 14


     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why would you expect that order? The call to adjustVar() comes before the println(var2).
     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought it was a matter of scope. I tested it in such a way that then led me to question all this again. I simply want to understand it.

    I am having a challenging time seeing the sequence of program flow in the context of variable scope. Is there a web application tool that shows the progression of sequence through a program during actual run-time? The closest thing that comes to mind is a debugging feature (perhaps within an IDE (eclipse)) and utilizing breakpoint step-overs. Wondering if there is something in addition to that, or even more detailed, which visibly traces a java run time sequence flow.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Scope (as in the general concept of "variable scope"--it sucks that the variable itself is named "scope") has nothing to do with this. It's simply a matter of execution flow:



    We print out "var1".
    Then we call adjustVar().***
    Then we printout "var2".


    ***When we call a method, that method's body is executed before we go on to the next step. Since adjustVar() prints out "var3" and "var4", that happens before we print out "var2".

    It's like if somebody tells you "Do your homework, then eat supper." The "Do your homework" step involves many smaller steps. We could write it as:


    When we follow the "do your homework" instruction, we do all of those individual steps before moving on to "eat supper." (And of course, each of those steps is broken down further into substeps, and so on.)
     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It makes a bit more sense. I tried to do a print test on scope.adjustVar in the main method and an error showed up. So, should I be surprised that I can call the variable but not print it?




    C:\src>javac VarScope1.java
    VarScope1.java:12: error: cannot find symbol
    System.out.println("varTest = " + adjustVar );
    ^
    symbol: variable adjustVar
    location: class VarScope1
    1 error

    C:\src>javac VarScope1.java
    VarScope1.java:12: error: cannot find symbol
    System.out.println("varTest = " + scope.adjustVar );
    ^
    symbol: variable adjustVar
    location: variable scope of type VarScope1
    1 error
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't know what you mean. There's no such thing as "calling" a variable. Either you can refer to a variable, or you cannot. Period.

    EDIT: Oh, I see what you mean. adjustVar is a method, not a variable. They are two completely different things, and it's vitally important that you understand the difference between them.

    Methods are named blocks of code that execute instructions, and that may or may not return a result from that execution. They are the verbs when we describe what we're doing in English. You can't print a method or assign a value to it.

    Variables represent data. They are the nouns when we describe what we're doing in English.
     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That was mis-typed, meant method. The adjustVar method. One thing I may be doing is trying to figure out why things seem so twisted, when all this really is - is attempts to teach things. To not have things sequentially written, but called out of order. Even though as far as Java is concerned it is in order. The order of the language specifications.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    David Starr wrote:That was mis-typed, meant method. The adjustVar method.


    See my edit above.
     
    David Starr
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The fact that 'scope' and 'var' are peppered in this code really tests my understanding. I am going to have to continue to tear this apart and test it more. I get what you are saying now as far as things being called out of chronological sequence (but are actually sequenced correctly as far as Java).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!