What is the problem with parameters in the constructor of a Class in Java?










2















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;











share|improve this question
























  • 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















2















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;











share|improve this question
























  • 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













2












2








2








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;











share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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












3 Answers
3






active

oldest

votes


















6














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;






share|improve this answer






























    1














    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=0or this.i=0. Same thing as there is only one i, so thisis 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 iof X, to the ipassed 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 ithe value 20. If your parameter was j, as jis not defined in your class, it takes the ias the iof 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;







    share|improve this answer






























      0














      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.






      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%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









        6














        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;






        share|improve this answer



























          6














          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;






          share|improve this answer

























            6












            6








            6







            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;






            share|improve this answer













            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;







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 7:58









            EranEran

            287k37467557




            287k37467557























                1














                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=0or this.i=0. Same thing as there is only one i, so thisis 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 iof X, to the ipassed 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 ithe value 20. If your parameter was j, as jis not defined in your class, it takes the ias the iof 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;







                share|improve this answer



























                  1














                  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=0or this.i=0. Same thing as there is only one i, so thisis 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 iof X, to the ipassed 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 ithe value 20. If your parameter was j, as jis not defined in your class, it takes the ias the iof 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;







                  share|improve this answer

























                    1












                    1








                    1







                    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=0or this.i=0. Same thing as there is only one i, so thisis 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 iof X, to the ipassed 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 ithe value 20. If your parameter was j, as jis not defined in your class, it takes the ias the iof 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;







                    share|improve this answer













                    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=0or this.i=0. Same thing as there is only one i, so thisis 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 iof X, to the ipassed 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 ithe value 20. If your parameter was j, as jis not defined in your class, it takes the ias the iof 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;








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 8:07









                    M.KM.K

                    649624




                    649624





















                        0














                        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.






                        share|improve this answer



























                          0














                          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.






                          share|improve this answer

























                            0












                            0








                            0







                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 7:58









                            MarkMark

                            3,69921126




                            3,69921126



























                                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%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





















































                                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

                                Use pre created SQLite database for Android project in kotlin

                                Darth Vader #20

                                Ondo