Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: How does this piece of code access a member-variable without the dot notation?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default How does this piece of code access a member-variable without the dot notation?

    From this tutorial,

    Java Code:
    public class MountainBike extends Bicycle {
            
        // the MountainBike subclass has
        // one field
        public int seatHeight;
    
        // the MountainBike subclass has
        // one constructor
        public MountainBike(int startHeight, int startCadence,
                            int startSpeed, int startGear) {
            super(startCadence, startSpeed, startGear);
            seatHeight = startHeight;
        }   
            
        // the MountainBike subclass has
        // one method
        public void setHeight(int newValue) {
            seatHeight = newValue;
        }   
    
    }
    At first,
    Java Code:
    public int seatHeight;
    tells us that seatHeight is NOT a static field (because of the absence of static keyword).

    Whereas in the constructor, the absence of dot notation (like something like this.seatHeight) in
    Java Code:
    seatHeight = newValue;
    shows that it IS a non-member/static variable.

    How does this make sense?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How does this piece of code access a member-variable without the dot notation?

    You are confused by several things. The first is that lack of the use of this notation to qualify a member variable does not imply the variable is static. That keyword is normally used to disambiguate the assignment of like named variables.

    Java Code:
    public int foo;
    public MyConstructor(int foo) {
        foo = foo; // incorrect! simply assigns the local variable to itself.    
        this.foo = foo; // correct!  uses this keyword to assign local foo to member foo.
    }
    Also note that for member fields (also called instance fields) the following are the same.

    Java Code:
    public int myVar;  // instance field
    
    public MyConstructor(int foo) {
        this.myVar = foo; // both of these assignment are the same.  This keyword not really needed.
        myVar = foo;
    }
    Now for the static part. A static variable or method is one which may be accessed by qualifying it with the class name. It does not require an instance of the class to access it.

    Java Code:
    public class MyClass {
       public static int bar;
    }
    
    // later on...
    
    int a = MyClass.bar;  // access the static variable.
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: How does this piece of code access a member-variable without the dot notation?

    Firstly thank you for answering. It clarified most of the confusion.

    You are confused by several things. The first is that lack of the use of this notation to qualify a member variable does not imply the variable is static.
    Yeah, I used `this` as an example only ("like something like this.seatHeight"). What I meant/was thinking was that the use of dot operator with a instance name (it's called 'dot notation used to access member/instance variables' if I am not wrong) is required to access an instance variable.

    This is what I think:


    • Static variables are declared with the static keyword. Instance variables are declared simply without the static keyword. From that I inferred that any variable I see being declared without the static keyword will be an instance variable (which is correct, right?).
    • It's true that from outside a class, we access its static variables by using the class name appended to a dot operator. But from within the class, we can access it just without any dot notation, just like line # 14 in

      Java Code:
      class Foo {
          private int foo;
          private static int zoo=0;
      
          public Foo(int foo) {
              this.foo = foo;
          }
      
          public aMethod() {
              System.out.print(fooInstance.foo);
          }
      
          public anotherMethod() {
              System.out.print(zoo);
          }
      
          public static void main(String [] args) {
              Foo fooInstance = new Foo(30);
              ...
          }
      
      }
    • To access an instance variable, we have to use a notation like "instanceName.instanceVariableName" like line # 10 in the example.
    • Sometimes in a constructor, the name of a member of the instance (which that constructor is supposed to create/help in creating/whatever) variable (- by which I mean a variable declared without the static keyword in that class, like foo in the example above) which is going to be assigned a value in the constructor, and the parameter variable passed to the constructor which contains the value, are the same. In that case, we use the 'this' keyword (with the dot notation for purposes like readability), to refer to the instance being created by this constructor. Like line # 6 in the example.
    • Also note that for member fields (also called instance fields) the following are the same.

      Java Code:
      public int myVar;  // instance field
       
      public MyConstructor(int foo) {
          this.myVar = foo; // both of these assignment are the same.  This keyword not really needed.
          myVar = foo;
      }
      So ^^ is not required within the body of a constructor, because since the instance variable is Not created yet. So how will we use the name of the instance with the dot operator to access its field?

      But firstly this can not be the reason for not requiring the dot notation to access the member variables within the body of the constructor, because this being the reason sounds so bogus (for the lack of another way to express it).

      Secondly, what if a static variable is also accessed for some reason within the body of the constructor (if that's possible?) - what is the visual difference between a statement with a static variable and an instance variable within the body of a constructor?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How does this piece of code access a member-variable without the dot notation?

    Secondly, what if a static variable is also accessed for some reason within the body of the constructor (if that's possible?) - what is the visual difference between a statement with a static variable and an instance variable within the body of a constructor?
    There may not be a visual difference. Within a class that has both static and instance fields, you can't tell them apart visually. Some IDE's may change the font to help the programmer but just looking at them on paper you can't tell the difference. The difference is all about their behavior and how they are accessed. You may use a static field anywhere within a class (static or non-static methods). But you may only use an instance field within an instance method (unless it is another objects instance field and is qualified with the dot notation). Also, within a class where the static field is declared, you do not need to qualify it with the dot operator. It is also possible to import static classes to avoid having to qualify them. I tend not to do this as I want to have a way of knowing which class I am using at the moment.

    Finally, you may use an instance of a class to access a static field or method of the same class. But that is not considered good style and should be avoided.

    Regards,
    Jim
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: How does this piece of code access a member-variable without the dot notation?

    @Jim829 Thank you very much. I am clear about it now.

Similar Threads

  1. Need help with a piece of code
    By sneeak in forum New To Java
    Replies: 13
    Last Post: 08-24-2011, 10:51 AM
  2. Can someone clarify this piece of code for me.
    By DavidPynes in forum New To Java
    Replies: 2
    Last Post: 04-17-2011, 10:22 AM
  3. Member variable corruption
    By Rolf in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-28-2011, 08:24 PM
  4. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  5. small piece of code: cannot set a max
    By senca in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •