What does this comment in the EditText.java code mean?












1















I've looked through EditText.java , at line 109 , the comment below can be seen in the
getText() method:



// This can only happen during construction.


What is its purpose?



Thanks










share|improve this question




















  • 1





    The purpose of a comment is to tell a programmer (human being) something.

    – Andreas
    Nov 14 '18 at 2:21








  • 2





    text==null can happen only in constructor, it is not null once constructor finishes.

    – AIMIN PAN
    Nov 14 '18 at 2:24











  • @AIMINPAN thks, would you tell me more when you say it is not null once constructor finishes?

    – H. Riantsoa
    Nov 14 '18 at 2:33






  • 1





    @H.Riantsoa It means that after the constructor has finished executing, the text will never be null. If you don't understand the phrase "once constructor finishes" my recommendation is to head over to the Java official documentation, and read about Constructors; they are pretty important :) docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (in the URL's example, the variables initialized in the constructor of that Bicycle object, are guaranteed to be assigned after the constructor finishes (when all the code in the ctor executed).

    – Martin Marconcini
    Nov 14 '18 at 2:49


















1















I've looked through EditText.java , at line 109 , the comment below can be seen in the
getText() method:



// This can only happen during construction.


What is its purpose?



Thanks










share|improve this question




















  • 1





    The purpose of a comment is to tell a programmer (human being) something.

    – Andreas
    Nov 14 '18 at 2:21








  • 2





    text==null can happen only in constructor, it is not null once constructor finishes.

    – AIMIN PAN
    Nov 14 '18 at 2:24











  • @AIMINPAN thks, would you tell me more when you say it is not null once constructor finishes?

    – H. Riantsoa
    Nov 14 '18 at 2:33






  • 1





    @H.Riantsoa It means that after the constructor has finished executing, the text will never be null. If you don't understand the phrase "once constructor finishes" my recommendation is to head over to the Java official documentation, and read about Constructors; they are pretty important :) docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (in the URL's example, the variables initialized in the constructor of that Bicycle object, are guaranteed to be assigned after the constructor finishes (when all the code in the ctor executed).

    – Martin Marconcini
    Nov 14 '18 at 2:49
















1












1








1








I've looked through EditText.java , at line 109 , the comment below can be seen in the
getText() method:



// This can only happen during construction.


What is its purpose?



Thanks










share|improve this question
















I've looked through EditText.java , at line 109 , the comment below can be seen in the
getText() method:



// This can only happen during construction.


What is its purpose?



Thanks







java android android-edittext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:21









shmosel

36.1k43892




36.1k43892










asked Nov 14 '18 at 2:20









H. RiantsoaH. Riantsoa

326




326








  • 1





    The purpose of a comment is to tell a programmer (human being) something.

    – Andreas
    Nov 14 '18 at 2:21








  • 2





    text==null can happen only in constructor, it is not null once constructor finishes.

    – AIMIN PAN
    Nov 14 '18 at 2:24











  • @AIMINPAN thks, would you tell me more when you say it is not null once constructor finishes?

    – H. Riantsoa
    Nov 14 '18 at 2:33






  • 1





    @H.Riantsoa It means that after the constructor has finished executing, the text will never be null. If you don't understand the phrase "once constructor finishes" my recommendation is to head over to the Java official documentation, and read about Constructors; they are pretty important :) docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (in the URL's example, the variables initialized in the constructor of that Bicycle object, are guaranteed to be assigned after the constructor finishes (when all the code in the ctor executed).

    – Martin Marconcini
    Nov 14 '18 at 2:49
















  • 1





    The purpose of a comment is to tell a programmer (human being) something.

    – Andreas
    Nov 14 '18 at 2:21








  • 2





    text==null can happen only in constructor, it is not null once constructor finishes.

    – AIMIN PAN
    Nov 14 '18 at 2:24











  • @AIMINPAN thks, would you tell me more when you say it is not null once constructor finishes?

    – H. Riantsoa
    Nov 14 '18 at 2:33






  • 1





    @H.Riantsoa It means that after the constructor has finished executing, the text will never be null. If you don't understand the phrase "once constructor finishes" my recommendation is to head over to the Java official documentation, and read about Constructors; they are pretty important :) docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (in the URL's example, the variables initialized in the constructor of that Bicycle object, are guaranteed to be assigned after the constructor finishes (when all the code in the ctor executed).

    – Martin Marconcini
    Nov 14 '18 at 2:49










1




1





The purpose of a comment is to tell a programmer (human being) something.

– Andreas
Nov 14 '18 at 2:21







The purpose of a comment is to tell a programmer (human being) something.

– Andreas
Nov 14 '18 at 2:21






2




2





text==null can happen only in constructor, it is not null once constructor finishes.

– AIMIN PAN
Nov 14 '18 at 2:24





text==null can happen only in constructor, it is not null once constructor finishes.

– AIMIN PAN
Nov 14 '18 at 2:24













@AIMINPAN thks, would you tell me more when you say it is not null once constructor finishes?

– H. Riantsoa
Nov 14 '18 at 2:33





@AIMINPAN thks, would you tell me more when you say it is not null once constructor finishes?

– H. Riantsoa
Nov 14 '18 at 2:33




1




1





@H.Riantsoa It means that after the constructor has finished executing, the text will never be null. If you don't understand the phrase "once constructor finishes" my recommendation is to head over to the Java official documentation, and read about Constructors; they are pretty important :) docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (in the URL's example, the variables initialized in the constructor of that Bicycle object, are guaranteed to be assigned after the constructor finishes (when all the code in the ctor executed).

– Martin Marconcini
Nov 14 '18 at 2:49







@H.Riantsoa It means that after the constructor has finished executing, the text will never be null. If you don't understand the phrase "once constructor finishes" my recommendation is to head over to the Java official documentation, and read about Constructors; they are pretty important :) docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (in the URL's example, the variables initialized in the constructor of that Bicycle object, are guaranteed to be assigned after the constructor finishes (when all the code in the ctor executed).

– Martin Marconcini
Nov 14 '18 at 2:49














2 Answers
2






active

oldest

votes


















2














I will expand on Andreas' answer, tailored for this particular set of classes.



EditText is a subclass of TextView. That's why it's declared as:



class EditText extends TextView (Simplified…)



The EditText constructors, look like this:



public EditText(Context context) {
this(context, null);
}
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}


Notice how each constructor calls the other until the last (long) one.
That one is tricky, because it calls super(…) meaning it's now calling the super class' constructor (TextView).



EditText is then just a specialized TextView as we can infer from this; TextView also declares a variable called text, which EditText uses as well of course. When you ask an EditText for the text, it simply uses the code that the TextView already has and that you discovered:



@Override
public Editable getText() {
CharSequence text = super.getText();
// This can only happen during construction.
if (text == null) {
return null;
}

[rest of code removed for clarity]
}


So it asks its super (the TextView) for the text; why can this text be null? To answer that, you need to go look at the TextView itself (https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/TextView.java).



About 1000 lines of code later… the TextView declares its constructors; I'll paste one for simplicity but they are very simple:



public TextView(Context context) {
this(context, null);
}


Notice they again call this(…) and chain the construction… one constructor calls the other and so forth… until the last big one for TextView is reached…



@SuppressWarnings("deprecation")
public TextView(
Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
// TextView is important by default, unless app developer overrode attribute.
if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
}
setTextInternal("");
[rest of code removed for clarity]


Notice something, this constructor, calls setTextInternal(""), that looks suspicious! it's setting the text to be an empty string upon construction, so it would be impossible for a TextView to have a null text after this constructor finishes (you could use reflection… but let's not go there); since EditText constructors are chained and end up always calling the TextView constructors, this method setTextInternal("") is guaranteed to be called at some point during the construction of the object in Java. (that's what Constructors are and that's a Java contract that would break 1/2 of the world if it were unreliable!).



What does this method do?



Apparently, a bunch of internal things, among other…



// Update mText and mPrecomputed
private void setTextInternal(@Nullable CharSequence text) {
mText = text; // #### THIS IS WHERE IT SETS THE TEXT TO NON NULL ("").
mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
}


If you remember that EditText#getText() called super.getText() guess what that looks like in super? (the TextView):



public CharSequence getText() {
return mText;
}


That's right, the same mText reference/instance that setTextInternal() modified during the TextView construction.



That this is why during construction (and before setTextInternal("") is called, the field mText (that you access via getText()) can be null, but not after.



¯_(ツ)_/¯



In all honesty, I don't even know why they do this and the comment seems kinda silly, but as you have probably already guessed, these classes are thousands of lines of code and one tends to believe there must be a reason for this to exist; the best you can do is start analyzing the entire TextView class. Good luck with that. :)






share|improve this answer































    1














    The purpose of the comment is to tell any programmer that thinks the next statement is redundant, i.e. that text can never be null, that they are wrong, because it can happen during construction of the EditText object, so don't remove the statement.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292281%2fwhat-does-this-comment-in-the-edittext-java-code-mean%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I will expand on Andreas' answer, tailored for this particular set of classes.



      EditText is a subclass of TextView. That's why it's declared as:



      class EditText extends TextView (Simplified…)



      The EditText constructors, look like this:



      public EditText(Context context) {
      this(context, null);
      }
      public EditText(Context context, AttributeSet attrs) {
      this(context, attrs, com.android.internal.R.attr.editTextStyle);
      }
      public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
      this(context, attrs, defStyleAttr, 0);
      }
      public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
      }


      Notice how each constructor calls the other until the last (long) one.
      That one is tricky, because it calls super(…) meaning it's now calling the super class' constructor (TextView).



      EditText is then just a specialized TextView as we can infer from this; TextView also declares a variable called text, which EditText uses as well of course. When you ask an EditText for the text, it simply uses the code that the TextView already has and that you discovered:



      @Override
      public Editable getText() {
      CharSequence text = super.getText();
      // This can only happen during construction.
      if (text == null) {
      return null;
      }

      [rest of code removed for clarity]
      }


      So it asks its super (the TextView) for the text; why can this text be null? To answer that, you need to go look at the TextView itself (https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/TextView.java).



      About 1000 lines of code later… the TextView declares its constructors; I'll paste one for simplicity but they are very simple:



      public TextView(Context context) {
      this(context, null);
      }


      Notice they again call this(…) and chain the construction… one constructor calls the other and so forth… until the last big one for TextView is reached…



      @SuppressWarnings("deprecation")
      public TextView(
      Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
      super(context, attrs, defStyleAttr, defStyleRes);
      // TextView is important by default, unless app developer overrode attribute.
      if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
      setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
      }
      setTextInternal("");
      [rest of code removed for clarity]


      Notice something, this constructor, calls setTextInternal(""), that looks suspicious! it's setting the text to be an empty string upon construction, so it would be impossible for a TextView to have a null text after this constructor finishes (you could use reflection… but let's not go there); since EditText constructors are chained and end up always calling the TextView constructors, this method setTextInternal("") is guaranteed to be called at some point during the construction of the object in Java. (that's what Constructors are and that's a Java contract that would break 1/2 of the world if it were unreliable!).



      What does this method do?



      Apparently, a bunch of internal things, among other…



      // Update mText and mPrecomputed
      private void setTextInternal(@Nullable CharSequence text) {
      mText = text; // #### THIS IS WHERE IT SETS THE TEXT TO NON NULL ("").
      mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
      mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
      }


      If you remember that EditText#getText() called super.getText() guess what that looks like in super? (the TextView):



      public CharSequence getText() {
      return mText;
      }


      That's right, the same mText reference/instance that setTextInternal() modified during the TextView construction.



      That this is why during construction (and before setTextInternal("") is called, the field mText (that you access via getText()) can be null, but not after.



      ¯_(ツ)_/¯



      In all honesty, I don't even know why they do this and the comment seems kinda silly, but as you have probably already guessed, these classes are thousands of lines of code and one tends to believe there must be a reason for this to exist; the best you can do is start analyzing the entire TextView class. Good luck with that. :)






      share|improve this answer




























        2














        I will expand on Andreas' answer, tailored for this particular set of classes.



        EditText is a subclass of TextView. That's why it's declared as:



        class EditText extends TextView (Simplified…)



        The EditText constructors, look like this:



        public EditText(Context context) {
        this(context, null);
        }
        public EditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
        }
        public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
        }
        public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        }


        Notice how each constructor calls the other until the last (long) one.
        That one is tricky, because it calls super(…) meaning it's now calling the super class' constructor (TextView).



        EditText is then just a specialized TextView as we can infer from this; TextView also declares a variable called text, which EditText uses as well of course. When you ask an EditText for the text, it simply uses the code that the TextView already has and that you discovered:



        @Override
        public Editable getText() {
        CharSequence text = super.getText();
        // This can only happen during construction.
        if (text == null) {
        return null;
        }

        [rest of code removed for clarity]
        }


        So it asks its super (the TextView) for the text; why can this text be null? To answer that, you need to go look at the TextView itself (https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/TextView.java).



        About 1000 lines of code later… the TextView declares its constructors; I'll paste one for simplicity but they are very simple:



        public TextView(Context context) {
        this(context, null);
        }


        Notice they again call this(…) and chain the construction… one constructor calls the other and so forth… until the last big one for TextView is reached…



        @SuppressWarnings("deprecation")
        public TextView(
        Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // TextView is important by default, unless app developer overrode attribute.
        if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
        setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
        }
        setTextInternal("");
        [rest of code removed for clarity]


        Notice something, this constructor, calls setTextInternal(""), that looks suspicious! it's setting the text to be an empty string upon construction, so it would be impossible for a TextView to have a null text after this constructor finishes (you could use reflection… but let's not go there); since EditText constructors are chained and end up always calling the TextView constructors, this method setTextInternal("") is guaranteed to be called at some point during the construction of the object in Java. (that's what Constructors are and that's a Java contract that would break 1/2 of the world if it were unreliable!).



        What does this method do?



        Apparently, a bunch of internal things, among other…



        // Update mText and mPrecomputed
        private void setTextInternal(@Nullable CharSequence text) {
        mText = text; // #### THIS IS WHERE IT SETS THE TEXT TO NON NULL ("").
        mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
        mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
        }


        If you remember that EditText#getText() called super.getText() guess what that looks like in super? (the TextView):



        public CharSequence getText() {
        return mText;
        }


        That's right, the same mText reference/instance that setTextInternal() modified during the TextView construction.



        That this is why during construction (and before setTextInternal("") is called, the field mText (that you access via getText()) can be null, but not after.



        ¯_(ツ)_/¯



        In all honesty, I don't even know why they do this and the comment seems kinda silly, but as you have probably already guessed, these classes are thousands of lines of code and one tends to believe there must be a reason for this to exist; the best you can do is start analyzing the entire TextView class. Good luck with that. :)






        share|improve this answer


























          2












          2








          2







          I will expand on Andreas' answer, tailored for this particular set of classes.



          EditText is a subclass of TextView. That's why it's declared as:



          class EditText extends TextView (Simplified…)



          The EditText constructors, look like this:



          public EditText(Context context) {
          this(context, null);
          }
          public EditText(Context context, AttributeSet attrs) {
          this(context, attrs, com.android.internal.R.attr.editTextStyle);
          }
          public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
          this(context, attrs, defStyleAttr, 0);
          }
          public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
          }


          Notice how each constructor calls the other until the last (long) one.
          That one is tricky, because it calls super(…) meaning it's now calling the super class' constructor (TextView).



          EditText is then just a specialized TextView as we can infer from this; TextView also declares a variable called text, which EditText uses as well of course. When you ask an EditText for the text, it simply uses the code that the TextView already has and that you discovered:



          @Override
          public Editable getText() {
          CharSequence text = super.getText();
          // This can only happen during construction.
          if (text == null) {
          return null;
          }

          [rest of code removed for clarity]
          }


          So it asks its super (the TextView) for the text; why can this text be null? To answer that, you need to go look at the TextView itself (https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/TextView.java).



          About 1000 lines of code later… the TextView declares its constructors; I'll paste one for simplicity but they are very simple:



          public TextView(Context context) {
          this(context, null);
          }


          Notice they again call this(…) and chain the construction… one constructor calls the other and so forth… until the last big one for TextView is reached…



          @SuppressWarnings("deprecation")
          public TextView(
          Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
          // TextView is important by default, unless app developer overrode attribute.
          if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
          setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
          }
          setTextInternal("");
          [rest of code removed for clarity]


          Notice something, this constructor, calls setTextInternal(""), that looks suspicious! it's setting the text to be an empty string upon construction, so it would be impossible for a TextView to have a null text after this constructor finishes (you could use reflection… but let's not go there); since EditText constructors are chained and end up always calling the TextView constructors, this method setTextInternal("") is guaranteed to be called at some point during the construction of the object in Java. (that's what Constructors are and that's a Java contract that would break 1/2 of the world if it were unreliable!).



          What does this method do?



          Apparently, a bunch of internal things, among other…



          // Update mText and mPrecomputed
          private void setTextInternal(@Nullable CharSequence text) {
          mText = text; // #### THIS IS WHERE IT SETS THE TEXT TO NON NULL ("").
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          If you remember that EditText#getText() called super.getText() guess what that looks like in super? (the TextView):



          public CharSequence getText() {
          return mText;
          }


          That's right, the same mText reference/instance that setTextInternal() modified during the TextView construction.



          That this is why during construction (and before setTextInternal("") is called, the field mText (that you access via getText()) can be null, but not after.



          ¯_(ツ)_/¯



          In all honesty, I don't even know why they do this and the comment seems kinda silly, but as you have probably already guessed, these classes are thousands of lines of code and one tends to believe there must be a reason for this to exist; the best you can do is start analyzing the entire TextView class. Good luck with that. :)






          share|improve this answer













          I will expand on Andreas' answer, tailored for this particular set of classes.



          EditText is a subclass of TextView. That's why it's declared as:



          class EditText extends TextView (Simplified…)



          The EditText constructors, look like this:



          public EditText(Context context) {
          this(context, null);
          }
          public EditText(Context context, AttributeSet attrs) {
          this(context, attrs, com.android.internal.R.attr.editTextStyle);
          }
          public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
          this(context, attrs, defStyleAttr, 0);
          }
          public EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
          }


          Notice how each constructor calls the other until the last (long) one.
          That one is tricky, because it calls super(…) meaning it's now calling the super class' constructor (TextView).



          EditText is then just a specialized TextView as we can infer from this; TextView also declares a variable called text, which EditText uses as well of course. When you ask an EditText for the text, it simply uses the code that the TextView already has and that you discovered:



          @Override
          public Editable getText() {
          CharSequence text = super.getText();
          // This can only happen during construction.
          if (text == null) {
          return null;
          }

          [rest of code removed for clarity]
          }


          So it asks its super (the TextView) for the text; why can this text be null? To answer that, you need to go look at the TextView itself (https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/TextView.java).



          About 1000 lines of code later… the TextView declares its constructors; I'll paste one for simplicity but they are very simple:



          public TextView(Context context) {
          this(context, null);
          }


          Notice they again call this(…) and chain the construction… one constructor calls the other and so forth… until the last big one for TextView is reached…



          @SuppressWarnings("deprecation")
          public TextView(
          Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
          // TextView is important by default, unless app developer overrode attribute.
          if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
          setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
          }
          setTextInternal("");
          [rest of code removed for clarity]


          Notice something, this constructor, calls setTextInternal(""), that looks suspicious! it's setting the text to be an empty string upon construction, so it would be impossible for a TextView to have a null text after this constructor finishes (you could use reflection… but let's not go there); since EditText constructors are chained and end up always calling the TextView constructors, this method setTextInternal("") is guaranteed to be called at some point during the construction of the object in Java. (that's what Constructors are and that's a Java contract that would break 1/2 of the world if it were unreliable!).



          What does this method do?



          Apparently, a bunch of internal things, among other…



          // Update mText and mPrecomputed
          private void setTextInternal(@Nullable CharSequence text) {
          mText = text; // #### THIS IS WHERE IT SETS THE TEXT TO NON NULL ("").
          mSpannable = (text instanceof Spannable) ? (Spannable) text : null;
          mPrecomputed = (text instanceof PrecomputedText) ? (PrecomputedText) text : null;
          }


          If you remember that EditText#getText() called super.getText() guess what that looks like in super? (the TextView):



          public CharSequence getText() {
          return mText;
          }


          That's right, the same mText reference/instance that setTextInternal() modified during the TextView construction.



          That this is why during construction (and before setTextInternal("") is called, the field mText (that you access via getText()) can be null, but not after.



          ¯_(ツ)_/¯



          In all honesty, I don't even know why they do this and the comment seems kinda silly, but as you have probably already guessed, these classes are thousands of lines of code and one tends to believe there must be a reason for this to exist; the best you can do is start analyzing the entire TextView class. Good luck with that. :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 3:21









          Martin MarconciniMartin Marconcini

          15.9k1487120




          15.9k1487120

























              1














              The purpose of the comment is to tell any programmer that thinks the next statement is redundant, i.e. that text can never be null, that they are wrong, because it can happen during construction of the EditText object, so don't remove the statement.






              share|improve this answer




























                1














                The purpose of the comment is to tell any programmer that thinks the next statement is redundant, i.e. that text can never be null, that they are wrong, because it can happen during construction of the EditText object, so don't remove the statement.






                share|improve this answer


























                  1












                  1








                  1







                  The purpose of the comment is to tell any programmer that thinks the next statement is redundant, i.e. that text can never be null, that they are wrong, because it can happen during construction of the EditText object, so don't remove the statement.






                  share|improve this answer













                  The purpose of the comment is to tell any programmer that thinks the next statement is redundant, i.e. that text can never be null, that they are wrong, because it can happen during construction of the EditText object, so don't remove the statement.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 2:33









                  AndreasAndreas

                  76.6k464124




                  76.6k464124






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292281%2fwhat-does-this-comment-in-the-edittext-java-code-mean%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      The Sandy Post

                      Danny Elfman

                      Pages that link to "Head v. Amoskeag Manufacturing Co."