Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

This keyword in Java

  1. Oct 8, 2011 #1
    Hello everyone. I want explanation of the working of 'this' keyword in java. I read articles but couldn't understand. Please help me here. I don't understand what objects and variables they are talking about. Also give me an example of what object, variable, instance variable is.

    Box(double w, double h, double d) {
    this.width = w;
    this.height = h;
    this.depth = d;

    In the above example, is width height depth an object or variable?? Explain me the 'this' keyword here please.
     
  2. jcsd
  3. Oct 8, 2011 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Writing code like that is a common convention especially for constructor functions.

    Actually your example is a bit silly. A better example would be
    Box(double width, double height, double depth) {
    this.width = width;
    this.height = height;
    this.depth = depth;

    In that example there are two different variables called "width". One of them will have been declared in the class. The other one is the parameter to the constructor.

    Within the constructor, the parameters are local variables and therefore the parameter "width" "hides" the class variable with the same name. "width" refers to the parameter, and "this.width" refers to the class variable. "this" just means "the name of the object of type Box that this function call refers to".

    Your original example would work just as well without using "this":

    Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;

    because all the variable names are unique anyway. But using "this" makes it clear which variables belong to the class and which are local to the function.
     
    Last edited: Oct 8, 2011
  4. Oct 8, 2011 #3
    An object is an instance of a class. A variable is a general term for a named "bucket" that you can keep a value inside (int number, float precise_number, String mySentence - these are all variables, and they all have a different type which is important, so the computer has some idea of what kind of variable you are talking about. Words are treated differently from numbers.) An instance variable, ok so you have a class (a template), and from this class you instantiate objects (you say, computer take this class design (of a box say), and build me an object (a box). Build as many boxes as you want. Box1, Box2, Box3 - all box objects created from the Box class. Each Box object has it's own copies of the variables that make up a box (width, height, depth). If you change one, you don't affect the other Boxes, because they are instance variables, related to the particular instance (Box1, Box2, Box3) of the class (Box)

    Here's a small example:

    Code (Text):
    public class Box
    {
        private:
        int width, height, depth;

        public Box(int width, height, depth)
        {
            this.width = width;
            this.height = height;
            this.depth = depth;
        }

        int getVolume()
        {
            volume = width * height * depth;
            return volume;
        }

        public static void main()
        {
            Box Box1 = new Box(10, 10, 10);
            Box Box2 = new Box(20, 20, 20);
            Box Box3 = new Box(30, 30, 30);

            int Vol1 = Box1.getVolume();
            int Vol2 = Box2.getVolume();
        }
    }
    Here I declare a class called Box. It contains a constructor, which is a special function to initialize the variables when you instantiate the class. What does that mean? In the main() function, refer to the line that says "Box Box1 = new Box(10, 10, 10);" This is an example of creating an object from a class definition, or instantiating the class, or creating an instance of the class. Before this, your class is just a template (a box design). After you create an object with the keyword new you have something useable, an actual Box.

    You can see that we call getVolume() on two different instances of the Box class (Box1, Box2). They each have different width, height and depth, hence they will return a different volume, because their instance variables contain different values. Same class, different object (or instance).

    The keyword this is a way of referring to the current object. See this page for a pretty good explanation with an example:

    http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html

    You can also use it to call the basic constructor if you have multiple (overloaded) constructors. The keyword super is closely related to this, it refers to the super-class, or parent-class, you might not be up to that just yet.

    Excuse my Java if I made any glaring mistakes, it's been a while. :P
     
    Last edited: Oct 8, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: This keyword in Java
  1. JQuery 'this' keyword (Replies: 2)

  2. Java interpreter (Replies: 8)

  3. Java Cylinders (Replies: 2)

Loading...