The role of this class name Object Name in Java

Source: Internet
Author: User

 

This indicates the "current object" and cannot be used in static methods.

As you know, there are many pulling objects, variable pulling, threads, and so on.

1. This is used to indicate global variables.
Public class testclass {int X, Y; void testshow (int x, int y) {This. X = x; // This. X is the X in int X, Y. X is the X this. Y = y ;}} from us ;}}

2.

public class TestClass {   int x;  int y;  static void showtest(TestClass tc){   System.out.println(tc.x+"  "+tc.y);  }  void seeit(){    showtest(this);  }  public static void main(String[] args){    TestClass p=new TestClass();    p.x=9;    p.y=10;    p.seeit();   }} 
In this Code, showtest (this), where this is to pass the current instantiated P to the showtest method, and thus it runs.

 

The class name is used to access static members;

Public class test {

Private float F = 1.0f;

Int M = 12;

Static int n = 1;

// Public static void main (string [] ARGs ){

Public void main (string [] argc ){

Test T = new test ();

Test. n = 10;

}

}

Object names can access static and non-static members

Public class test {

Private float F = 1.0f;

Int M = 12;

Static int n = 1;

// Public static void main (string [] ARGs ){

Public void main (string [] argc ){

Test T = new test ();

Test. n = 10;

T. M = 10;

T. N = 10;

}

}

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.