What is the purpose of StatementWithEmptyBody?












8















when I created the drawer bar in Android Studio for my project, in the code for the select items put the next:



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {


But what is the use of this annotation?










share|improve this question

























  • Its just indicate empty block of code ,just like your methods is empty , no use of that

    – Chetan Joshi
    Feb 5 at 11:08
















8















when I created the drawer bar in Android Studio for my project, in the code for the select items put the next:



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {


But what is the use of this annotation?










share|improve this question

























  • Its just indicate empty block of code ,just like your methods is empty , no use of that

    – Chetan Joshi
    Feb 5 at 11:08














8












8








8


1






when I created the drawer bar in Android Studio for my project, in the code for the select items put the next:



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {


But what is the use of this annotation?










share|improve this question
















when I created the drawer bar in Android Studio for my project, in the code for the select items put the next:



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {


But what is the use of this annotation?







android suppress-warnings android-annotations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 5 at 11:07









Michael Dodd

5,953103855




5,953103855










asked Feb 5 at 11:00









Tlaloc-ESTlaloc-ES

147117




147117













  • Its just indicate empty block of code ,just like your methods is empty , no use of that

    – Chetan Joshi
    Feb 5 at 11:08



















  • Its just indicate empty block of code ,just like your methods is empty , no use of that

    – Chetan Joshi
    Feb 5 at 11:08

















Its just indicate empty block of code ,just like your methods is empty , no use of that

– Chetan Joshi
Feb 5 at 11:08





Its just indicate empty block of code ,just like your methods is empty , no use of that

– Chetan Joshi
Feb 5 at 11:08












4 Answers
4






active

oldest

votes


















4














Warning itself explains meaning.



return type of onNavigationItemSelected is boolean. and we need to return any boolean value.



If there is if condition in onNavigationItemSelected and not returned then @SuppressWarnings("StatementWithEmptyBody") need to add.



Example:



@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.xyz) {
// you should return boolean value here.
}
return false;
}


In example we are returning false by-default. and we haven't return any value in



if (id == R.id.xyz) condition.



You can clearly have a look at warning.



suppress warning






share|improve this answer


























  • But what happens if you not put the annotation, can fail the app?

    – Tlaloc-ES
    Feb 5 at 11:28






  • 1





    Of course not. it's just warning same as other warnings.

    – Rumit Patel
    Feb 5 at 11:35






  • 2





    boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

    – pts
    Feb 5 at 16:41






  • 1





    @pts, mistake corrected. thanks for drawing attention.

    – Rumit Patel
    Feb 5 at 17:13



















4














In case, if you have not inserted any code in that method, the annotation will make sure that no warnings are generated for that method.






share|improve this answer
























  • But is a function with return value, if the body is empty, appear a error for no return

    – Tlaloc-ES
    Feb 5 at 11:06











  • The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

    – fatema sagar
    Feb 5 at 11:12



















3














When you have a statement with no code inside of the curly brackets, Android Studio will give a warning about this. The annotation suppresses this warning in method having this annotation.



Example of statement with empty body (the body of the "else" part is empty):



if (something) {
doThis();
} else {

}


These warnings are useful to let you double check if you didn't forget to code something. Only turn them off (with the annotation) when you have a good reason to do so.






share|improve this answer































    3














    This guards against things like:



    if(condition)
    {
    //empty body
    }


    OR



    private void methodeName(){
    //empty body
    }


    The error raised forces you to explicitly put the empty {}



    I should probably clarify that suppressing a warning that does have merit is a silly thing to do. A clean bill of health that you obtained by cheating is obviously worth nothing. Given the choice, you should always fix the problem the compiler noticed rather than just close your eyes to it. However, there are areas in which the compiler cannot be sure whether something will be a problem or not (Java's generics are one such area), and there the better choice is to review each such instance and then suppress the warning in this specific place rather than to switch off this class of warning altogether and potentially miss a genuine one.






    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%2f54532928%2fwhat-is-the-purpose-of-statementwithemptybody%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Warning itself explains meaning.



      return type of onNavigationItemSelected is boolean. and we need to return any boolean value.



      If there is if condition in onNavigationItemSelected and not returned then @SuppressWarnings("StatementWithEmptyBody") need to add.



      Example:



      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.xyz) {
      // you should return boolean value here.
      }
      return false;
      }


      In example we are returning false by-default. and we haven't return any value in



      if (id == R.id.xyz) condition.



      You can clearly have a look at warning.



      suppress warning






      share|improve this answer


























      • But what happens if you not put the annotation, can fail the app?

        – Tlaloc-ES
        Feb 5 at 11:28






      • 1





        Of course not. it's just warning same as other warnings.

        – Rumit Patel
        Feb 5 at 11:35






      • 2





        boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

        – pts
        Feb 5 at 16:41






      • 1





        @pts, mistake corrected. thanks for drawing attention.

        – Rumit Patel
        Feb 5 at 17:13
















      4














      Warning itself explains meaning.



      return type of onNavigationItemSelected is boolean. and we need to return any boolean value.



      If there is if condition in onNavigationItemSelected and not returned then @SuppressWarnings("StatementWithEmptyBody") need to add.



      Example:



      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.xyz) {
      // you should return boolean value here.
      }
      return false;
      }


      In example we are returning false by-default. and we haven't return any value in



      if (id == R.id.xyz) condition.



      You can clearly have a look at warning.



      suppress warning






      share|improve this answer


























      • But what happens if you not put the annotation, can fail the app?

        – Tlaloc-ES
        Feb 5 at 11:28






      • 1





        Of course not. it's just warning same as other warnings.

        – Rumit Patel
        Feb 5 at 11:35






      • 2





        boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

        – pts
        Feb 5 at 16:41






      • 1





        @pts, mistake corrected. thanks for drawing attention.

        – Rumit Patel
        Feb 5 at 17:13














      4












      4








      4







      Warning itself explains meaning.



      return type of onNavigationItemSelected is boolean. and we need to return any boolean value.



      If there is if condition in onNavigationItemSelected and not returned then @SuppressWarnings("StatementWithEmptyBody") need to add.



      Example:



      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.xyz) {
      // you should return boolean value here.
      }
      return false;
      }


      In example we are returning false by-default. and we haven't return any value in



      if (id == R.id.xyz) condition.



      You can clearly have a look at warning.



      suppress warning






      share|improve this answer















      Warning itself explains meaning.



      return type of onNavigationItemSelected is boolean. and we need to return any boolean value.



      If there is if condition in onNavigationItemSelected and not returned then @SuppressWarnings("StatementWithEmptyBody") need to add.



      Example:



      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.xyz) {
      // you should return boolean value here.
      }
      return false;
      }


      In example we are returning false by-default. and we haven't return any value in



      if (id == R.id.xyz) condition.



      You can clearly have a look at warning.



      suppress warning







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 5 at 17:12

























      answered Feb 5 at 11:14









      Rumit PatelRumit Patel

      1,87852536




      1,87852536













      • But what happens if you not put the annotation, can fail the app?

        – Tlaloc-ES
        Feb 5 at 11:28






      • 1





        Of course not. it's just warning same as other warnings.

        – Rumit Patel
        Feb 5 at 11:35






      • 2





        boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

        – pts
        Feb 5 at 16:41






      • 1





        @pts, mistake corrected. thanks for drawing attention.

        – Rumit Patel
        Feb 5 at 17:13



















      • But what happens if you not put the annotation, can fail the app?

        – Tlaloc-ES
        Feb 5 at 11:28






      • 1





        Of course not. it's just warning same as other warnings.

        – Rumit Patel
        Feb 5 at 11:35






      • 2





        boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

        – pts
        Feb 5 at 16:41






      • 1





        @pts, mistake corrected. thanks for drawing attention.

        – Rumit Patel
        Feb 5 at 17:13

















      But what happens if you not put the annotation, can fail the app?

      – Tlaloc-ES
      Feb 5 at 11:28





      But what happens if you not put the annotation, can fail the app?

      – Tlaloc-ES
      Feb 5 at 11:28




      1




      1





      Of course not. it's just warning same as other warnings.

      – Rumit Patel
      Feb 5 at 11:35





      Of course not. it's just warning same as other warnings.

      – Rumit Patel
      Feb 5 at 11:35




      2




      2





      boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

      – pts
      Feb 5 at 16:41





      boolean and Boolean are different types in Java. This function should return a boolean. This answer incorrectly states that this function should return a Boolean.

      – pts
      Feb 5 at 16:41




      1




      1





      @pts, mistake corrected. thanks for drawing attention.

      – Rumit Patel
      Feb 5 at 17:13





      @pts, mistake corrected. thanks for drawing attention.

      – Rumit Patel
      Feb 5 at 17:13













      4














      In case, if you have not inserted any code in that method, the annotation will make sure that no warnings are generated for that method.






      share|improve this answer
























      • But is a function with return value, if the body is empty, appear a error for no return

        – Tlaloc-ES
        Feb 5 at 11:06











      • The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

        – fatema sagar
        Feb 5 at 11:12
















      4














      In case, if you have not inserted any code in that method, the annotation will make sure that no warnings are generated for that method.






      share|improve this answer
























      • But is a function with return value, if the body is empty, appear a error for no return

        – Tlaloc-ES
        Feb 5 at 11:06











      • The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

        – fatema sagar
        Feb 5 at 11:12














      4












      4








      4







      In case, if you have not inserted any code in that method, the annotation will make sure that no warnings are generated for that method.






      share|improve this answer













      In case, if you have not inserted any code in that method, the annotation will make sure that no warnings are generated for that method.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Feb 5 at 11:04









      fatema sagarfatema sagar

      373




      373













      • But is a function with return value, if the body is empty, appear a error for no return

        – Tlaloc-ES
        Feb 5 at 11:06











      • The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

        – fatema sagar
        Feb 5 at 11:12



















      • But is a function with return value, if the body is empty, appear a error for no return

        – Tlaloc-ES
        Feb 5 at 11:06











      • The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

        – fatema sagar
        Feb 5 at 11:12

















      But is a function with return value, if the body is empty, appear a error for no return

      – Tlaloc-ES
      Feb 5 at 11:06





      But is a function with return value, if the body is empty, appear a error for no return

      – Tlaloc-ES
      Feb 5 at 11:06













      The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

      – fatema sagar
      Feb 5 at 11:12





      The method is generated when You added the drawer bar, which is a default method. Once you start adding options in your Menu Item to the Menu, only then it will generate any error.

      – fatema sagar
      Feb 5 at 11:12











      3














      When you have a statement with no code inside of the curly brackets, Android Studio will give a warning about this. The annotation suppresses this warning in method having this annotation.



      Example of statement with empty body (the body of the "else" part is empty):



      if (something) {
      doThis();
      } else {

      }


      These warnings are useful to let you double check if you didn't forget to code something. Only turn them off (with the annotation) when you have a good reason to do so.






      share|improve this answer




























        3














        When you have a statement with no code inside of the curly brackets, Android Studio will give a warning about this. The annotation suppresses this warning in method having this annotation.



        Example of statement with empty body (the body of the "else" part is empty):



        if (something) {
        doThis();
        } else {

        }


        These warnings are useful to let you double check if you didn't forget to code something. Only turn them off (with the annotation) when you have a good reason to do so.






        share|improve this answer


























          3












          3








          3







          When you have a statement with no code inside of the curly brackets, Android Studio will give a warning about this. The annotation suppresses this warning in method having this annotation.



          Example of statement with empty body (the body of the "else" part is empty):



          if (something) {
          doThis();
          } else {

          }


          These warnings are useful to let you double check if you didn't forget to code something. Only turn them off (with the annotation) when you have a good reason to do so.






          share|improve this answer













          When you have a statement with no code inside of the curly brackets, Android Studio will give a warning about this. The annotation suppresses this warning in method having this annotation.



          Example of statement with empty body (the body of the "else" part is empty):



          if (something) {
          doThis();
          } else {

          }


          These warnings are useful to let you double check if you didn't forget to code something. Only turn them off (with the annotation) when you have a good reason to do so.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 5 at 11:14









          Wouter Van der SchraelenWouter Van der Schraelen

          115312




          115312























              3














              This guards against things like:



              if(condition)
              {
              //empty body
              }


              OR



              private void methodeName(){
              //empty body
              }


              The error raised forces you to explicitly put the empty {}



              I should probably clarify that suppressing a warning that does have merit is a silly thing to do. A clean bill of health that you obtained by cheating is obviously worth nothing. Given the choice, you should always fix the problem the compiler noticed rather than just close your eyes to it. However, there are areas in which the compiler cannot be sure whether something will be a problem or not (Java's generics are one such area), and there the better choice is to review each such instance and then suppress the warning in this specific place rather than to switch off this class of warning altogether and potentially miss a genuine one.






              share|improve this answer




























                3














                This guards against things like:



                if(condition)
                {
                //empty body
                }


                OR



                private void methodeName(){
                //empty body
                }


                The error raised forces you to explicitly put the empty {}



                I should probably clarify that suppressing a warning that does have merit is a silly thing to do. A clean bill of health that you obtained by cheating is obviously worth nothing. Given the choice, you should always fix the problem the compiler noticed rather than just close your eyes to it. However, there are areas in which the compiler cannot be sure whether something will be a problem or not (Java's generics are one such area), and there the better choice is to review each such instance and then suppress the warning in this specific place rather than to switch off this class of warning altogether and potentially miss a genuine one.






                share|improve this answer


























                  3












                  3








                  3







                  This guards against things like:



                  if(condition)
                  {
                  //empty body
                  }


                  OR



                  private void methodeName(){
                  //empty body
                  }


                  The error raised forces you to explicitly put the empty {}



                  I should probably clarify that suppressing a warning that does have merit is a silly thing to do. A clean bill of health that you obtained by cheating is obviously worth nothing. Given the choice, you should always fix the problem the compiler noticed rather than just close your eyes to it. However, there are areas in which the compiler cannot be sure whether something will be a problem or not (Java's generics are one such area), and there the better choice is to review each such instance and then suppress the warning in this specific place rather than to switch off this class of warning altogether and potentially miss a genuine one.






                  share|improve this answer













                  This guards against things like:



                  if(condition)
                  {
                  //empty body
                  }


                  OR



                  private void methodeName(){
                  //empty body
                  }


                  The error raised forces you to explicitly put the empty {}



                  I should probably clarify that suppressing a warning that does have merit is a silly thing to do. A clean bill of health that you obtained by cheating is obviously worth nothing. Given the choice, you should always fix the problem the compiler noticed rather than just close your eyes to it. However, there are areas in which the compiler cannot be sure whether something will be a problem or not (Java's generics are one such area), and there the better choice is to review each such instance and then suppress the warning in this specific place rather than to switch off this class of warning altogether and potentially miss a genuine one.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 5 at 11:22









                  PraveenPraveen

                  566213




                  566213






























                      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%2f54532928%2fwhat-is-the-purpose-of-statementwithemptybody%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

                      Human spaceflight

                      Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                      張江高科駅