Java object-Oriented learning notes (1)

Source: Internet
Author: User

This note is used to document the Java object-oriented part of the learning experience.


I. Object's this pointer reference

Java provides a the This keyword, which always points to the object that called the method. Depending on where this occurs, this is the default reference for an object in two cases:

1. The object that the constructor is initializing is referenced in the constructor.

2. In the method refers to the object that called the method.

The maximum effect of the This keyword is to have one of the methods in the class access another method or file. This is illustrated by a section of code below.


Package Test;public class Dog {public void jump () {System.out.println ("Executing jump Method");} public void Run () {/*  dog dog=new Dog ();  Dog. Jum (); *////this. Jump (); Jump (); SYSTEM.OUT.PRINTLN ("Executing the Run Method");}   public static void Main (string[] args) {      dog d = new Dog ();   D.run ();   }}

two methods are defined in the Dog class run () and jump (); What if we want to call jump () in the Run () method? First, we know that a method that uses static modifiers and a file must be called through an object. One way to do this is to declare a dog object in the Run () method and then invoke its jump () method; But there is actually a problem, we declare an object to call run (), and then declare an object in run () to call jump (), In this case, run () and jump () do not belong to the same object, this will make people feel very awkward; in fact, we can pass thisto achieve our goal.

This can represent any object, and when this appears in a method body, the object it represents is indeterminate, but its type is deterministic, and the object that represents it can only be the current class. When invoked only with this method, the object it represents can be determined: who is calling this method, this is who it stands for. Of course, when writing code, we can usually omit it.

We know that the method in Java that specifies static adornments cannot access ordinary members that do not use static. The reason for this is that a static decorated member is called by a class, which means that it does not have a corresponding object when it is called, and this cannot refer to a valid object if we call a non-static member through this in the static decorated method. So what if we want to reference a normal variable in some static method? We can declare an object in the static method, and then the reference is OK.

It is generally possible to omit the this prefix when accessing other file and methods in constructors and normal methods, but if the constructor or normal method has a local variable with the same name as the file, the overridden file must be accessed in the constructor, and the this prefix must be added. As in the following code:


<span style= "FONT-SIZE:18PX;" >package Test;public class Thisinconstructor {public int foo;public thisinconstructor () {//define an foo variable int foo=0;/in the constructor /Use this to initialize the object for this constructor///The code below will set the Foo of the newly created object to 6this.foo=6;} public static void Main (string[] args) {/////The Foo file//for an object created using Thisinconstructor is set to 6, so the following code outputs 6SYSTEM.OUT.PRINTLN ( New Thisinconstructor (). foo);}} </span>



Java object-Oriented learning notes (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.