Meaning of using this in the constructor method

Source: Internet
Author: User

Today, using the This keyword in the constructor method, or feeling confused in the principle, the This keyword represents this object of this class. There are two types of ways to use in a construction method.

The first class uses this to call other constructors, which is nothing to say.

Public Nodetree (context context) throws IllegalArgumentException, illegalaccessexception{this (context, NULL, 0);}    Public Nodetree (context context, AttributeSet attrs, int defstyle) throws IllegalArgumentException, illegalaccessexception {    Super (context, attrs, defstyle);        LOG.D ("TAG", "Nodetree");        This.context = context;    InitData ();    Initcontent (context,10);    }

The second class is to use the This keyword for the current object's various fields (field) initialization, and then the above code, multiple times use this, write the time also wondered if the current object has not been created, can you go to AddView () (Nodetree is inherited from LinearLayout)

private void Initcontent (final Context context,int defaultexpandlevel) throws IllegalArgumentException, illegalaccessexception {log.d ("TAG", "initcontent");//TODO auto-generated method Stub mallnodes = Treehelp        Er.getsortednodes (MDATAS2, defaultexpandlevel);    Mvisinodes = Treehelper.filtervisiblenode (mallnodes);        This.setorientation (linearlayout.vertical);        ListView = new ListView (context); Linearlayout.layoutparams params =new linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT,        linearlayout.layoutparams.match_parent,1);        Listview.setlayoutparams (params);        This.addview (ListView);        relayout= new Relativelayout (context); Linearlayout.layoutparams params1 =new linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT,        linearlayout.layoutparams.match_parent,6);        Relayout.setlayoutparams (PARAMS1);        Relayout.setid (0x1234);        Relayout.setgravity (gravity.center_vertical); Fillbuttonutil.fillButtons (context, relayout);        This.addview (relayout); Madapter = new Simpletreeadapter<filebean> (ListView, context, this); Listview.setadapter (Madapter);}

In fact, the work of the Java Virtual machine is not familiar with the process, in the creation of the object (new), JM first in the method area to create a request for a piece of memory space, which has an object header, the storage of the object's class information, address information, GC collection generation and other information, this time is just an empty object. Before entering into the construction method, for the current object of the various fields or the properties of the various aspects of the initialization, the process to add a child is not a problem, in memory is the performance of the object's memory space will be more than a pointer to record the child's relevant information.

Meaning of using this in the constructor method

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.