How to call a private method that initializes a char array inside constructor java
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
add a comment |
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
3
Just callthis.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
IsinitializeGardenpart 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
add a comment |
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
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
java
asked Nov 23 at 3:23
mtlchk
72
72
3
Just callthis.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
IsinitializeGardenpart 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
add a comment |
3
Just callthis.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
IsinitializeGardenpart 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
add a comment |
3 Answers
3
active
oldest
votes
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();
}
}
add a comment |
Your private method intializeGarden appears to have a typo in it.
So the call would look like intializeGarden(garden)
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
add a comment |
Simply change
this.initializeGarden(garden);
to
this.initializeGarden(garden);
The above code will pass the garden variable as an argument to the initializeGarden method.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
}
}
add a comment |
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();
}
}
add a comment |
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();
}
}
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();
}
}
answered Nov 23 at 4:07
0day
557
557
add a comment |
add a comment |
Your private method intializeGarden appears to have a typo in it.
So the call would look like intializeGarden(garden)
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
add a comment |
Your private method intializeGarden appears to have a typo in it.
So the call would look like intializeGarden(garden)
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
add a comment |
Your private method intializeGarden appears to have a typo in it.
So the call would look like intializeGarden(garden)
Your private method intializeGarden appears to have a typo in it.
So the call would look like intializeGarden(garden)
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
add a comment |
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
add a comment |
Simply change
this.initializeGarden(garden);
to
this.initializeGarden(garden);
The above code will pass the garden variable as an argument to the initializeGarden method.
add a comment |
Simply change
this.initializeGarden(garden);
to
this.initializeGarden(garden);
The above code will pass the garden variable as an argument to the initializeGarden method.
add a comment |
Simply change
this.initializeGarden(garden);
to
this.initializeGarden(garden);
The above code will pass the garden variable as an argument to the initializeGarden method.
Simply change
this.initializeGarden(garden);
to
this.initializeGarden(garden);
The above code will pass the garden variable as an argument to the initializeGarden method.
answered Nov 23 at 3:37
hev1
5,5533527
5,5533527
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
initializeGardenpart 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