Java keywords: this, super, Static__java

Source: Internet
Author: User
This

This can only be used within a method, representing a reference to the object that invoked the method or property.

Public class MyClass {

Private String name;

Public MyClass () {}

Public MyClass (String name) {

This (); Must be at the top of this constructor, and a constructor can only be used once for this

this. Name = name;

}

Public void fun1 () {

String s = this. Name;

this. fun2 ();

}

Public void fun2 () {}

} Static

static data, which cannot invoke non-static data, can be reversed.

Static can modify variables, blocks of code, methods, classes (only internal anonymous classes).

Public class MyClass {

private static String name;

Public static void fun () {}

Public static void main (string[] args) {

System.out.println (Myclass.name);

Myclass.fun ();

}

}

Static data is initialized when class is loader; the initialization of the property of the class is initialized before the constructor call, so the following code, the value of I, first 0, then constructs the method to modify it to 7.

Public class MyClass {

private int i;

Public MyClass () {

i = 7;

}

} Super

Super and this, you can refer to constructors, properties, and methods, but are all of the parent classes:

Public class MyClass extends A {

Public MyClass () {

Super (); constructor, must be top, one

}

void Fun () {

System.out.println (Super. i); Property

Super. Funa (); Method

}

}

class A

int i;

Public A () {}

void Funa () {}

}

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.