Monday, June 20, 2016

Keyword this

In certain cases, the method is sometimes necessary to refer to object that has it. To allow this, Java provides keyword this. Keyword this is actually a reference to the object that is currently active. Consider the following code:

class Box {
   
   double length, width, height;

   //constructor
   Box(double l, double w, double h) {
      length = l;
      width = w;
      height = h;
   }
}

Actually, the above code can also be written using reference this, as follows:

class Box {
   
   double length, width, height;

   //constructor
   Box(double l, double w, double h) {
      this.length = l;
      this.width = w;
      this.height = h;
   }
}

For the case above, the presence of keyword this is actually going to be a little inconvenient and can be said to be unnecessary. However, in other contexts, the use of keyword this is actually required. As proof of this statement, you try to take a good look at the explanation below.

As we all know, Java does not allow us to declare variable with the same name within a block. On the other hand, Java allows us to declare name of the same parameter as the name of the data contained in the class. Nevertheless, the name of a local variable (in this case, the variable used as a parameter) will overwrite with the name of the data contained in the class. To avoid overwriting process variable, then we must use keyword this, as shown in the following code:

class Box {

   double length, width, height;

   //constructor
   Box(double length, double width, double height) {
      this.length = length;
      this.width = width;
      this.height = height;
   }
}

Here, this.length, this.width, and this.height refer to data length, width, and height contained in the class Box, not refer to parameters contained in the constructor.

9 comments:

  1. Local Cash for Car is a licensed auto removal company that buys all types of used, junk, scrap, broken, and damaged vehicles for as much as up to Cash For Scrap Cars Mitchelton

    ReplyDelete
  2. Thanks for sharing such a great post I like your way of writing. We purchase any type of old, damaged, scrap, junk, wrecked, and unwanted vehicles for as much as up to $10000 One of the prime benefits of choosing us is that you will also get free car removal anywhere in EuroaCash for Cars Melbourne

    ReplyDelete
  3. We offer the best value for money. We provide same-day car removal throughout Brisbane and surrounding suburbs. Brisbane Car Removalists can offer the highest possible cash settlement for your vehicle no matter the make, model, or condition of the car. We will take Utes, vans, trucks, undrivable and unregistered cars and will pay up to $5,999. As a result, this makes us one of the most competitive car buyers in Brisbanefree car removal brisbane

    ReplyDelete
  4. Cash for Cars Underwood is a licensed company in QLD. We are experienced in a wide variety of services such as car removal, cash for scrap cars, eco-friendly car recycling, spare parts, etc. With years of experience in this industry, our professional team of experts is well aware of the changing trends and practices. Armed with this knowledge we provide our customers with the top quality service.cash for scrap cars underwood

    ReplyDelete
  5. There are many places to sell your car, and the best option is cash for cars brisbane. These services pick up your vehicle and pay top cash for it. In most cases, you can even sell your wrecked or old 4WD for instant cash. These companies can buy any car in any condition, including those that are not roadworthy or have no region. Here are a few of them: a.

    ReplyDelete
  6. If you're looking for a company that offers free car removal goldcoast, look no further than Free Car Removal Gold Coast. Their friendly, knowledgeable customer care agents are dedicated to giving you the best cash payout for your junk vehicle. Additionally, they'll come to your location and provide a towing service for your convenience. This will free up valuable parking space. And what's more, you can get rid of your old car without even leaving your home!

    ReplyDelete


  7. Great post you shared with lots of important points Thank you for sharing.I was really enjoying reading this article.

    led tv repairing course in Delhi
    tv repairing course in Delhi

    ReplyDelete
  8. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Dwarka

    ReplyDelete