How to call a private method that initializes a char array inside constructor java












0














I am working on my final project in my OOP1 class. The language is java.



I'd like to know how I invoke the following method inside my constructor:



public Garden (int size)    {

garden=new char[size][size];

this.initializeGarden(garden);
}


private void intializeGarden(char garden) {

for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';

}


this.initializeGarden(garden); is one of several failed attempts.
I've tried a few variations, and eclipse didn't like any of them.










share|improve this question


















  • 3




    Just call this.initializeGarden(garden), without the brackets.
    – Jai
    Nov 23 at 3:27






  • 1




    Possible duplicate of Can I call methods in constructor in Java?
    – user7
    Nov 23 at 3:27






  • 1




    Have a look at Why is it considered bad practice to call a method from within a constructor
    – user7
    Nov 23 at 3:28










  • Is initializeGarden part of the class? Also, the in that function call inside of the constructor is not needed. If you are getting any errors, could you edit your answer to include them?
    – Jonathan Van Dam
    Nov 23 at 3:28
















0














I am working on my final project in my OOP1 class. The language is java.



I'd like to know how I invoke the following method inside my constructor:



public Garden (int size)    {

garden=new char[size][size];

this.initializeGarden(garden);
}


private void intializeGarden(char garden) {

for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';

}


this.initializeGarden(garden); is one of several failed attempts.
I've tried a few variations, and eclipse didn't like any of them.










share|improve this question


















  • 3




    Just call this.initializeGarden(garden), without the brackets.
    – Jai
    Nov 23 at 3:27






  • 1




    Possible duplicate of Can I call methods in constructor in Java?
    – user7
    Nov 23 at 3:27






  • 1




    Have a look at Why is it considered bad practice to call a method from within a constructor
    – user7
    Nov 23 at 3:28










  • Is initializeGarden part of the class? Also, the in that function call inside of the constructor is not needed. If you are getting any errors, could you edit your answer to include them?
    – Jonathan Van Dam
    Nov 23 at 3:28














0












0








0







I am working on my final project in my OOP1 class. The language is java.



I'd like to know how I invoke the following method inside my constructor:



public Garden (int size)    {

garden=new char[size][size];

this.initializeGarden(garden);
}


private void intializeGarden(char garden) {

for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';

}


this.initializeGarden(garden); is one of several failed attempts.
I've tried a few variations, and eclipse didn't like any of them.










share|improve this question













I am working on my final project in my OOP1 class. The language is java.



I'd like to know how I invoke the following method inside my constructor:



public Garden (int size)    {

garden=new char[size][size];

this.initializeGarden(garden);
}


private void intializeGarden(char garden) {

for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';

}


this.initializeGarden(garden); is one of several failed attempts.
I've tried a few variations, and eclipse didn't like any of them.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 3:23









mtlchk

72




72








  • 3




    Just call this.initializeGarden(garden), without the brackets.
    – Jai
    Nov 23 at 3:27






  • 1




    Possible duplicate of Can I call methods in constructor in Java?
    – user7
    Nov 23 at 3:27






  • 1




    Have a look at Why is it considered bad practice to call a method from within a constructor
    – user7
    Nov 23 at 3:28










  • Is initializeGarden part of the class? Also, the in that function call inside of the constructor is not needed. If you are getting any errors, could you edit your answer to include them?
    – Jonathan Van Dam
    Nov 23 at 3:28














  • 3




    Just call this.initializeGarden(garden), without the brackets.
    – Jai
    Nov 23 at 3:27






  • 1




    Possible duplicate of Can I call methods in constructor in Java?
    – user7
    Nov 23 at 3:27






  • 1




    Have a look at Why is it considered bad practice to call a method from within a constructor
    – user7
    Nov 23 at 3:28










  • Is initializeGarden part of the class? Also, the in that function call inside of the constructor is not needed. If you are getting any errors, could you edit your answer to include them?
    – Jonathan Van Dam
    Nov 23 at 3:28








3




3




Just call this.initializeGarden(garden), without the brackets.
– Jai
Nov 23 at 3:27




Just call this.initializeGarden(garden), without the brackets.
– Jai
Nov 23 at 3:27




1




1




Possible duplicate of Can I call methods in constructor in Java?
– user7
Nov 23 at 3:27




Possible duplicate of Can I call methods in constructor in Java?
– user7
Nov 23 at 3:27




1




1




Have a look at Why is it considered bad practice to call a method from within a constructor
– user7
Nov 23 at 3:28




Have a look at Why is it considered bad practice to call a method from within a constructor
– user7
Nov 23 at 3:28












Is initializeGarden part of the class? Also, the in that function call inside of the constructor is not needed. If you are getting any errors, could you edit your answer to include them?
– Jonathan Van Dam
Nov 23 at 3:28




Is initializeGarden part of the class? Also, the in that function call inside of the constructor is not needed. If you are getting any errors, could you edit your answer to include them?
– Jonathan Van Dam
Nov 23 at 3:28












3 Answers
3






active

oldest

votes


















2














public class Garden {
char garden;

public Garden (int size) {

garden=new char[size][size];

this.initializeGarden(garden);
}


private void initializeGarden(char garden) {

for(int i=0;i<garden.length;i++)
for(int j =0;j<garden.length;j++)
garden[i][j]='-';

}

public void display(){
for(int i=0;i<garden.length;i++){
for(int j =0;j<garden.length;j++){
System.out.print(garden[i][j]);
}
System.out.println();
}


}


public static void main(String args) {
new Garden(20).display();
}
}





share|improve this answer





























    0














    Your private method intializeGarden appears to have a typo in it.



    So the call would look like intializeGarden(garden)






    share|improve this answer





















    • Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
      – mtlchk
      Nov 23 at 3:49





















    0














    Simply change



    this.initializeGarden(garden);


    to



    this.initializeGarden(garden);


    The above code will pass the garden variable as an argument to the initializeGarden method.






    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%2f53440294%2fhow-to-call-a-private-method-that-initializes-a-char-array-inside-constructor-ja%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      public class Garden {
      char garden;

      public Garden (int size) {

      garden=new char[size][size];

      this.initializeGarden(garden);
      }


      private void initializeGarden(char garden) {

      for(int i=0;i<garden.length;i++)
      for(int j =0;j<garden.length;j++)
      garden[i][j]='-';

      }

      public void display(){
      for(int i=0;i<garden.length;i++){
      for(int j =0;j<garden.length;j++){
      System.out.print(garden[i][j]);
      }
      System.out.println();
      }


      }


      public static void main(String args) {
      new Garden(20).display();
      }
      }





      share|improve this answer


























        2














        public class Garden {
        char garden;

        public Garden (int size) {

        garden=new char[size][size];

        this.initializeGarden(garden);
        }


        private void initializeGarden(char garden) {

        for(int i=0;i<garden.length;i++)
        for(int j =0;j<garden.length;j++)
        garden[i][j]='-';

        }

        public void display(){
        for(int i=0;i<garden.length;i++){
        for(int j =0;j<garden.length;j++){
        System.out.print(garden[i][j]);
        }
        System.out.println();
        }


        }


        public static void main(String args) {
        new Garden(20).display();
        }
        }





        share|improve this answer
























          2












          2








          2






          public class Garden {
          char garden;

          public Garden (int size) {

          garden=new char[size][size];

          this.initializeGarden(garden);
          }


          private void initializeGarden(char garden) {

          for(int i=0;i<garden.length;i++)
          for(int j =0;j<garden.length;j++)
          garden[i][j]='-';

          }

          public void display(){
          for(int i=0;i<garden.length;i++){
          for(int j =0;j<garden.length;j++){
          System.out.print(garden[i][j]);
          }
          System.out.println();
          }


          }


          public static void main(String args) {
          new Garden(20).display();
          }
          }





          share|improve this answer












          public class Garden {
          char garden;

          public Garden (int size) {

          garden=new char[size][size];

          this.initializeGarden(garden);
          }


          private void initializeGarden(char garden) {

          for(int i=0;i<garden.length;i++)
          for(int j =0;j<garden.length;j++)
          garden[i][j]='-';

          }

          public void display(){
          for(int i=0;i<garden.length;i++){
          for(int j =0;j<garden.length;j++){
          System.out.print(garden[i][j]);
          }
          System.out.println();
          }


          }


          public static void main(String args) {
          new Garden(20).display();
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 4:07









          0day

          557




          557

























              0














              Your private method intializeGarden appears to have a typo in it.



              So the call would look like intializeGarden(garden)






              share|improve this answer





















              • Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
                – mtlchk
                Nov 23 at 3:49


















              0














              Your private method intializeGarden appears to have a typo in it.



              So the call would look like intializeGarden(garden)






              share|improve this answer





















              • Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
                – mtlchk
                Nov 23 at 3:49
















              0












              0








              0






              Your private method intializeGarden appears to have a typo in it.



              So the call would look like intializeGarden(garden)






              share|improve this answer












              Your private method intializeGarden appears to have a typo in it.



              So the call would look like intializeGarden(garden)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 at 3:33









              TT--

              630723




              630723












              • Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
                – mtlchk
                Nov 23 at 3:49




















              • Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
                – mtlchk
                Nov 23 at 3:49


















              Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
              – mtlchk
              Nov 23 at 3:49






              Yes, I saw that after posting. Thanks :) Well, at least I was not totally out to lunch. "Hmm, this should work" Yeah it does, when you spell things right!
              – mtlchk
              Nov 23 at 3:49













              0














              Simply change



              this.initializeGarden(garden);


              to



              this.initializeGarden(garden);


              The above code will pass the garden variable as an argument to the initializeGarden method.






              share|improve this answer


























                0














                Simply change



                this.initializeGarden(garden);


                to



                this.initializeGarden(garden);


                The above code will pass the garden variable as an argument to the initializeGarden method.






                share|improve this answer
























                  0












                  0








                  0






                  Simply change



                  this.initializeGarden(garden);


                  to



                  this.initializeGarden(garden);


                  The above code will pass the garden variable as an argument to the initializeGarden method.






                  share|improve this answer












                  Simply change



                  this.initializeGarden(garden);


                  to



                  this.initializeGarden(garden);


                  The above code will pass the garden variable as an argument to the initializeGarden method.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 at 3:37









                  hev1

                  5,5533527




                  5,5533527






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53440294%2fhow-to-call-a-private-method-that-initializes-a-char-array-inside-constructor-ja%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

                      Lallio

                      Futebolista

                      Jornalista