What is the problem with parameters in the constructor of a Class in Java?
In this code if I keep int i
in the parameterized constructor, it throws an error. If anything other than int i
is working fine.
Example: int j
works fine. What is the reason for this error, please enlighten my knowledge.
//this program throws an error
class X
final int i;
X()
i = 0;
X(int i)//need to keep other than i
i = 20;
//this program works fine
class X
final int i;
X()
i = 0;
X(int j)
i = 20;
java constructor final
add a comment |
In this code if I keep int i
in the parameterized constructor, it throws an error. If anything other than int i
is working fine.
Example: int j
works fine. What is the reason for this error, please enlighten my knowledge.
//this program throws an error
class X
final int i;
X()
i = 0;
X(int i)//need to keep other than i
i = 20;
//this program works fine
class X
final int i;
X()
i = 0;
X(int j)
i = 20;
java constructor final
I think you needthis.i = 20;
– Sweeper
Nov 14 '18 at 7:57
2
I think you already have the correct answer, but for future reference it would be better to say what error you're seeing.
– DaveyDaveDave
Nov 14 '18 at 8:10
add a comment |
In this code if I keep int i
in the parameterized constructor, it throws an error. If anything other than int i
is working fine.
Example: int j
works fine. What is the reason for this error, please enlighten my knowledge.
//this program throws an error
class X
final int i;
X()
i = 0;
X(int i)//need to keep other than i
i = 20;
//this program works fine
class X
final int i;
X()
i = 0;
X(int j)
i = 20;
java constructor final
In this code if I keep int i
in the parameterized constructor, it throws an error. If anything other than int i
is working fine.
Example: int j
works fine. What is the reason for this error, please enlighten my knowledge.
//this program throws an error
class X
final int i;
X()
i = 0;
X(int i)//need to keep other than i
i = 20;
//this program works fine
class X
final int i;
X()
i = 0;
X(int j)
i = 20;
java constructor final
java constructor final
edited Nov 14 '18 at 12:29
Marcel Bro
2,86422849
2,86422849
asked Nov 14 '18 at 7:55
vijay_kyvijay_ky
114
114
I think you needthis.i = 20;
– Sweeper
Nov 14 '18 at 7:57
2
I think you already have the correct answer, but for future reference it would be better to say what error you're seeing.
– DaveyDaveDave
Nov 14 '18 at 8:10
add a comment |
I think you needthis.i = 20;
– Sweeper
Nov 14 '18 at 7:57
2
I think you already have the correct answer, but for future reference it would be better to say what error you're seeing.
– DaveyDaveDave
Nov 14 '18 at 8:10
I think you need
this.i = 20;
– Sweeper
Nov 14 '18 at 7:57
I think you need
this.i = 20;
– Sweeper
Nov 14 '18 at 7:57
2
2
I think you already have the correct answer, but for future reference it would be better to say what error you're seeing.
– DaveyDaveDave
Nov 14 '18 at 8:10
I think you already have the correct answer, but for future reference it would be better to say what error you're seeing.
– DaveyDaveDave
Nov 14 '18 at 8:10
add a comment |
3 Answers
3
active
oldest
votes
In
X(int i)
i = 20;
The local variable i
(the argument of the constructor) hides the instance variable i
. Therefore i = 20;
modifies the local variable, not the final
instance variable of the same name, which remains uninitialized.
You can avoid this issue with:
X(int i)
this.i = 20;
or
X(int i)
this.i = i;
add a comment |
class X
final int i;
X()
i = 0;
What you are saying above is basically that your i
is equal to 0 if no parameters are given. It is the same as saying i=0
or this.i=0
. Same thing as there is only one i
, so this
is not really needed.
On the other side, below, there are 2 instances of i
, so you need to make sure Java knows which one is which one. In this case, imagine you wanted to equal the i
of X
, to the i
passed as a parameter: this.i=i;
is what you would be looking for. "My i
of the class should be the i
passed as a parameter.
So in this case, if your constructor receives an int, it should give YOUR i
the value 20
. If your parameter was j
, as j
is not defined in your class, it takes the i
as the i
of your class. But having 2 i
, you should make sure which one is which one!
X(int i)//need to keep other than i
//the i of my class should be 20. remember! this.i=i if you want the i of your class to be equal to the parameter passed i
this.i = 20;
add a comment |
In the code above you're never initializing the variable final int i
which should either be done when declaring this variable or in the constructors of your class, even though you're doing i = 20;
you're refering to the int i
which is part of the parameters of that method.
If you wanted to change the value of final int i
and have a parameter named i
in your method, you have to call this.i = 20;
and it will be fixed.
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%2f53295376%2fwhat-is-the-problem-with-parameters-in-the-constructor-of-a-class-in-java%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
In
X(int i)
i = 20;
The local variable i
(the argument of the constructor) hides the instance variable i
. Therefore i = 20;
modifies the local variable, not the final
instance variable of the same name, which remains uninitialized.
You can avoid this issue with:
X(int i)
this.i = 20;
or
X(int i)
this.i = i;
add a comment |
In
X(int i)
i = 20;
The local variable i
(the argument of the constructor) hides the instance variable i
. Therefore i = 20;
modifies the local variable, not the final
instance variable of the same name, which remains uninitialized.
You can avoid this issue with:
X(int i)
this.i = 20;
or
X(int i)
this.i = i;
add a comment |
In
X(int i)
i = 20;
The local variable i
(the argument of the constructor) hides the instance variable i
. Therefore i = 20;
modifies the local variable, not the final
instance variable of the same name, which remains uninitialized.
You can avoid this issue with:
X(int i)
this.i = 20;
or
X(int i)
this.i = i;
In
X(int i)
i = 20;
The local variable i
(the argument of the constructor) hides the instance variable i
. Therefore i = 20;
modifies the local variable, not the final
instance variable of the same name, which remains uninitialized.
You can avoid this issue with:
X(int i)
this.i = 20;
or
X(int i)
this.i = i;
answered Nov 14 '18 at 7:58
EranEran
287k37467557
287k37467557
add a comment |
add a comment |
class X
final int i;
X()
i = 0;
What you are saying above is basically that your i
is equal to 0 if no parameters are given. It is the same as saying i=0
or this.i=0
. Same thing as there is only one i
, so this
is not really needed.
On the other side, below, there are 2 instances of i
, so you need to make sure Java knows which one is which one. In this case, imagine you wanted to equal the i
of X
, to the i
passed as a parameter: this.i=i;
is what you would be looking for. "My i
of the class should be the i
passed as a parameter.
So in this case, if your constructor receives an int, it should give YOUR i
the value 20
. If your parameter was j
, as j
is not defined in your class, it takes the i
as the i
of your class. But having 2 i
, you should make sure which one is which one!
X(int i)//need to keep other than i
//the i of my class should be 20. remember! this.i=i if you want the i of your class to be equal to the parameter passed i
this.i = 20;
add a comment |
class X
final int i;
X()
i = 0;
What you are saying above is basically that your i
is equal to 0 if no parameters are given. It is the same as saying i=0
or this.i=0
. Same thing as there is only one i
, so this
is not really needed.
On the other side, below, there are 2 instances of i
, so you need to make sure Java knows which one is which one. In this case, imagine you wanted to equal the i
of X
, to the i
passed as a parameter: this.i=i;
is what you would be looking for. "My i
of the class should be the i
passed as a parameter.
So in this case, if your constructor receives an int, it should give YOUR i
the value 20
. If your parameter was j
, as j
is not defined in your class, it takes the i
as the i
of your class. But having 2 i
, you should make sure which one is which one!
X(int i)//need to keep other than i
//the i of my class should be 20. remember! this.i=i if you want the i of your class to be equal to the parameter passed i
this.i = 20;
add a comment |
class X
final int i;
X()
i = 0;
What you are saying above is basically that your i
is equal to 0 if no parameters are given. It is the same as saying i=0
or this.i=0
. Same thing as there is only one i
, so this
is not really needed.
On the other side, below, there are 2 instances of i
, so you need to make sure Java knows which one is which one. In this case, imagine you wanted to equal the i
of X
, to the i
passed as a parameter: this.i=i;
is what you would be looking for. "My i
of the class should be the i
passed as a parameter.
So in this case, if your constructor receives an int, it should give YOUR i
the value 20
. If your parameter was j
, as j
is not defined in your class, it takes the i
as the i
of your class. But having 2 i
, you should make sure which one is which one!
X(int i)//need to keep other than i
//the i of my class should be 20. remember! this.i=i if you want the i of your class to be equal to the parameter passed i
this.i = 20;
class X
final int i;
X()
i = 0;
What you are saying above is basically that your i
is equal to 0 if no parameters are given. It is the same as saying i=0
or this.i=0
. Same thing as there is only one i
, so this
is not really needed.
On the other side, below, there are 2 instances of i
, so you need to make sure Java knows which one is which one. In this case, imagine you wanted to equal the i
of X
, to the i
passed as a parameter: this.i=i;
is what you would be looking for. "My i
of the class should be the i
passed as a parameter.
So in this case, if your constructor receives an int, it should give YOUR i
the value 20
. If your parameter was j
, as j
is not defined in your class, it takes the i
as the i
of your class. But having 2 i
, you should make sure which one is which one!
X(int i)//need to keep other than i
//the i of my class should be 20. remember! this.i=i if you want the i of your class to be equal to the parameter passed i
this.i = 20;
answered Nov 14 '18 at 8:07
M.KM.K
649624
649624
add a comment |
add a comment |
In the code above you're never initializing the variable final int i
which should either be done when declaring this variable or in the constructors of your class, even though you're doing i = 20;
you're refering to the int i
which is part of the parameters of that method.
If you wanted to change the value of final int i
and have a parameter named i
in your method, you have to call this.i = 20;
and it will be fixed.
add a comment |
In the code above you're never initializing the variable final int i
which should either be done when declaring this variable or in the constructors of your class, even though you're doing i = 20;
you're refering to the int i
which is part of the parameters of that method.
If you wanted to change the value of final int i
and have a parameter named i
in your method, you have to call this.i = 20;
and it will be fixed.
add a comment |
In the code above you're never initializing the variable final int i
which should either be done when declaring this variable or in the constructors of your class, even though you're doing i = 20;
you're refering to the int i
which is part of the parameters of that method.
If you wanted to change the value of final int i
and have a parameter named i
in your method, you have to call this.i = 20;
and it will be fixed.
In the code above you're never initializing the variable final int i
which should either be done when declaring this variable or in the constructors of your class, even though you're doing i = 20;
you're refering to the int i
which is part of the parameters of that method.
If you wanted to change the value of final int i
and have a parameter named i
in your method, you have to call this.i = 20;
and it will be fixed.
answered Nov 14 '18 at 7:58
MarkMark
3,69921126
3,69921126
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.
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%2f53295376%2fwhat-is-the-problem-with-parameters-in-the-constructor-of-a-class-in-java%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
I think you need
this.i = 20;
– Sweeper
Nov 14 '18 at 7:57
2
I think you already have the correct answer, but for future reference it would be better to say what error you're seeing.
– DaveyDaveDave
Nov 14 '18 at 8:10