Go: Use of Java class name this

Source: Internet
Author: User

Class name.

We know that in Java, when a class is loaded, the virtual machine automatically generates a class object of the class type of the classes, each of which corresponds to a class object, and through this class object, we can use the "introspection and reflection" mechanism , access the information of a class, such as: What are the methods in the corresponding class, what are the member fields, and so on; one way to get the class object of a class is by using the class name. Class this way returns an object of class type, and the other way to get the class object is as follows:

1). Gets the class instance of the object by invoking the GetClass () method with the object
2). Use the static method of Class forname () to get a class instance by its name
3). Use the. Calss method to obtain a class instance, a wrapper class for the base data type, and a. Type to get a class instance of the corresponding base data type.

The following is the Testclass.java code:

  1. Public class TestClass {
  2. public static void Main (string[] args) {
  3. //During runtime, if we want to produce an object of a class, the Java virtual Opportunity detects whether the class object of that type has been loaded. If not loaded, the Java virtual opportunity finds the. class file and loads it based on the name of the class.
  4. //When new point () is loaded, the class is loaded when the instance is constructed with forname. Load only once
  5. System.out.println ("before New Point ()");
  6. new Point ();
  7. System.out.println ("after new Point ()");
  8. try {
  9. Class.forName ("line");
  10. } catch (Exception e) {
  11. E.printstacktrace ();
  12. }
  13. //Use the object call GetClass () method to get the class instance of the object
  14. Point pt = new Point ();
  15. Class C1 = Pt.getclass ();
  16. System.out.println (C1.getname ()); //Result: point
  17. //Use the class's static method Forname () to get a classes instance using its name
  18. try {
  19. Class C2 = class.forname ("point");
  20. System.out.println (C2.getname ()); //Result: point
  21. Point pp = (point) c2.newinstance (); //Once a class object of a type has been loaded into memory, it can be used to produce all objects of that type.
  22. //newinstance () invokes the default constructor method in the class.
  23. Pp.output ();
  24. } catch (Exception e) {
  25. E.printstacktrace ();
  26. }
  27. //Use. class to get class instance (Class)
  28. Class C3 = Point. class;
  29. System.out.println (C3.getname ()); //Result: point
  30. //Use the. Calss method to obtain a class instance (base type)
  31. Class C4 = int.class;
  32. System.out.println (C4.getname ()); //Result: int
  33. //Use. class to get the class instance (encapsulation class for the base data type)
  34. Class c5 = integer.type;
  35. System.out.println (C5.getname ()); //Result: int
  36. Class C6 = Integer. class;
  37. System.out.println (C6.getname ()); //Result: Java.lang.Integer
  38. }
  39. }
  40. Class Point {
  41. Static {
  42. System.out.println ("Loading point");
  43. }
  44. void Output () {
  45. System.out.println ("x=" + x + ", y=" + y);
  46. }
  47. int x, y;
  48. }
  49. Class Line {
  50. Static {
  51. System.out.println ("Loading line");
  52. }
  53. }

class name.

There are two main aspects of the application of this grammar:

① when in the inner class of a class, if you need to access the method or member domain of an external class, use this. The member domain (as with the inner class. this. The member domain is not separate) is called the domain of the inner class, and if we want to access the domain of the outer class, we must use the External class. this. member domain

Package com.test;
public class TestA 
{   
public void tn ()
{      &N bsp; 
System.out.println ("External class TN");       
}  
thread thread = new Thread () {   
public void tn () {SYSTEM.OUT.PRINTLN ("inner TN"),}       
public void Run () {& nbsp        
System.out.println ("internal class Run");     &nbs p; 
TestA.this.tn ();//Call the TN method of the external class.         
This.tn ();//Call the TN method of the inner class       &N bsp;  
}   
};       
public static void Main (String aaa[])
{new TestA (). Thread.Start ();}
}
② also has a use case, that is, in the use of intent more clearly, in Android development we often have to use the Context type of parameters in some places, and this parameter we often use this

public class Mainactivity extends Activity {

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

Intent Intent = new Intent (mainactivity.this, Otheractivity.class);

}

}

This shows that the intent object we created is associated with an object of type mainactivity, which means that the intent is emitted by the Mainactivity object,

Well, this shows that there are cases where the class name is used. This is not the same as the direct use of this, but the class name is used. This can clearly show a correlation, so it is worth advocating

At the same time, if the intent we create is created in an anonymous inner class, but we want this to be logically associated with the Outer class object in the intent object, we must use the external class name.

public class Mainactivity extends Activity {

Private button button;

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

This.button = (Button) This.findviewbyid (R.ID.BUTTON01);

This.button.setOnClickListener (New Onclicklistener () {

@Override

public void OnClick (View v) {

Intent Intent = new Intent ();

Intent.setclass (Mainactivity.this, Nextactivity.class);

StartActivity (Intent);

}

});

}

}

Go: Use of Java class name this

Related Article

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.