How to set a variable in one class and get it in another












0















Once I have set the "set" method in one class to set my accessor, is it possible to return(get) that variable/string in another class without first having to "set" the variable again?



public class A {
Edits edits = new Edits("hello") }


now I want to access this from class B



public class B {
Edits edits = new Edits();
String hello = edits.getHello(); }


Problem is that there is and error initializing "new Edits()" because it first has to be set.










share|improve this question




















  • 1





    A class or an instance of a class? If it's a class, and you're talking about a static member, then yes, as long as the application is alive (which depends on the environment we're talking about). If it's an instance, yes as long as that instance is alive.

    – Federico klez Culloca
    Nov 14 '18 at 13:44











  • From another class, the problem is how to retrieve it without initializing the accessor again.

    – Emmanuel Conradie
    Nov 14 '18 at 13:46











  • I didn't make myself clear. Suppose you have a class named A with a static setter named setA(int a) and a static getter getA(). You can do A.setA(12) and then from inside another class you can do A.getA(). Is this what you're asking?

    – Federico klez Culloca
    Nov 14 '18 at 13:48













  • are you planning on only creating one instance of a class, like a singleton?

    – depperm
    Nov 14 '18 at 13:49











  • @ Federico yes exactly

    – Emmanuel Conradie
    Nov 14 '18 at 13:51
















0















Once I have set the "set" method in one class to set my accessor, is it possible to return(get) that variable/string in another class without first having to "set" the variable again?



public class A {
Edits edits = new Edits("hello") }


now I want to access this from class B



public class B {
Edits edits = new Edits();
String hello = edits.getHello(); }


Problem is that there is and error initializing "new Edits()" because it first has to be set.










share|improve this question




















  • 1





    A class or an instance of a class? If it's a class, and you're talking about a static member, then yes, as long as the application is alive (which depends on the environment we're talking about). If it's an instance, yes as long as that instance is alive.

    – Federico klez Culloca
    Nov 14 '18 at 13:44











  • From another class, the problem is how to retrieve it without initializing the accessor again.

    – Emmanuel Conradie
    Nov 14 '18 at 13:46











  • I didn't make myself clear. Suppose you have a class named A with a static setter named setA(int a) and a static getter getA(). You can do A.setA(12) and then from inside another class you can do A.getA(). Is this what you're asking?

    – Federico klez Culloca
    Nov 14 '18 at 13:48













  • are you planning on only creating one instance of a class, like a singleton?

    – depperm
    Nov 14 '18 at 13:49











  • @ Federico yes exactly

    – Emmanuel Conradie
    Nov 14 '18 at 13:51














0












0








0








Once I have set the "set" method in one class to set my accessor, is it possible to return(get) that variable/string in another class without first having to "set" the variable again?



public class A {
Edits edits = new Edits("hello") }


now I want to access this from class B



public class B {
Edits edits = new Edits();
String hello = edits.getHello(); }


Problem is that there is and error initializing "new Edits()" because it first has to be set.










share|improve this question
















Once I have set the "set" method in one class to set my accessor, is it possible to return(get) that variable/string in another class without first having to "set" the variable again?



public class A {
Edits edits = new Edits("hello") }


now I want to access this from class B



public class B {
Edits edits = new Edits();
String hello = edits.getHello(); }


Problem is that there is and error initializing "new Edits()" because it first has to be set.







java variables get set accessor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 21:54







Emmanuel Conradie

















asked Nov 14 '18 at 13:42









Emmanuel ConradieEmmanuel Conradie

66




66








  • 1





    A class or an instance of a class? If it's a class, and you're talking about a static member, then yes, as long as the application is alive (which depends on the environment we're talking about). If it's an instance, yes as long as that instance is alive.

    – Federico klez Culloca
    Nov 14 '18 at 13:44











  • From another class, the problem is how to retrieve it without initializing the accessor again.

    – Emmanuel Conradie
    Nov 14 '18 at 13:46











  • I didn't make myself clear. Suppose you have a class named A with a static setter named setA(int a) and a static getter getA(). You can do A.setA(12) and then from inside another class you can do A.getA(). Is this what you're asking?

    – Federico klez Culloca
    Nov 14 '18 at 13:48













  • are you planning on only creating one instance of a class, like a singleton?

    – depperm
    Nov 14 '18 at 13:49











  • @ Federico yes exactly

    – Emmanuel Conradie
    Nov 14 '18 at 13:51














  • 1





    A class or an instance of a class? If it's a class, and you're talking about a static member, then yes, as long as the application is alive (which depends on the environment we're talking about). If it's an instance, yes as long as that instance is alive.

    – Federico klez Culloca
    Nov 14 '18 at 13:44











  • From another class, the problem is how to retrieve it without initializing the accessor again.

    – Emmanuel Conradie
    Nov 14 '18 at 13:46











  • I didn't make myself clear. Suppose you have a class named A with a static setter named setA(int a) and a static getter getA(). You can do A.setA(12) and then from inside another class you can do A.getA(). Is this what you're asking?

    – Federico klez Culloca
    Nov 14 '18 at 13:48













  • are you planning on only creating one instance of a class, like a singleton?

    – depperm
    Nov 14 '18 at 13:49











  • @ Federico yes exactly

    – Emmanuel Conradie
    Nov 14 '18 at 13:51








1




1





A class or an instance of a class? If it's a class, and you're talking about a static member, then yes, as long as the application is alive (which depends on the environment we're talking about). If it's an instance, yes as long as that instance is alive.

– Federico klez Culloca
Nov 14 '18 at 13:44





A class or an instance of a class? If it's a class, and you're talking about a static member, then yes, as long as the application is alive (which depends on the environment we're talking about). If it's an instance, yes as long as that instance is alive.

– Federico klez Culloca
Nov 14 '18 at 13:44













From another class, the problem is how to retrieve it without initializing the accessor again.

– Emmanuel Conradie
Nov 14 '18 at 13:46





From another class, the problem is how to retrieve it without initializing the accessor again.

– Emmanuel Conradie
Nov 14 '18 at 13:46













I didn't make myself clear. Suppose you have a class named A with a static setter named setA(int a) and a static getter getA(). You can do A.setA(12) and then from inside another class you can do A.getA(). Is this what you're asking?

– Federico klez Culloca
Nov 14 '18 at 13:48







I didn't make myself clear. Suppose you have a class named A with a static setter named setA(int a) and a static getter getA(). You can do A.setA(12) and then from inside another class you can do A.getA(). Is this what you're asking?

– Federico klez Culloca
Nov 14 '18 at 13:48















are you planning on only creating one instance of a class, like a singleton?

– depperm
Nov 14 '18 at 13:49





are you planning on only creating one instance of a class, like a singleton?

– depperm
Nov 14 '18 at 13:49













@ Federico yes exactly

– Emmanuel Conradie
Nov 14 '18 at 13:51





@ Federico yes exactly

– Emmanuel Conradie
Nov 14 '18 at 13:51












1 Answer
1






active

oldest

votes


















0














The answer here is actually quite simple. All you do it declare a static variable and call it with the class from another class.



public class Edits {
public static String edits;
}


Set it in another class



public class A {
Edits.edits = "new value";
}


Then get it from another class



public class B {
doSomething(Edits.edits);
}





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%2f53301644%2fhow-to-set-a-variable-in-one-class-and-get-it-in-another%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The answer here is actually quite simple. All you do it declare a static variable and call it with the class from another class.



    public class Edits {
    public static String edits;
    }


    Set it in another class



    public class A {
    Edits.edits = "new value";
    }


    Then get it from another class



    public class B {
    doSomething(Edits.edits);
    }





    share|improve this answer




























      0














      The answer here is actually quite simple. All you do it declare a static variable and call it with the class from another class.



      public class Edits {
      public static String edits;
      }


      Set it in another class



      public class A {
      Edits.edits = "new value";
      }


      Then get it from another class



      public class B {
      doSomething(Edits.edits);
      }





      share|improve this answer


























        0












        0








        0







        The answer here is actually quite simple. All you do it declare a static variable and call it with the class from another class.



        public class Edits {
        public static String edits;
        }


        Set it in another class



        public class A {
        Edits.edits = "new value";
        }


        Then get it from another class



        public class B {
        doSomething(Edits.edits);
        }





        share|improve this answer













        The answer here is actually quite simple. All you do it declare a static variable and call it with the class from another class.



        public class Edits {
        public static String edits;
        }


        Set it in another class



        public class A {
        Edits.edits = "new value";
        }


        Then get it from another class



        public class B {
        doSomething(Edits.edits);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 21:41









        Emmanuel ConradieEmmanuel Conradie

        66




        66
































            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%2f53301644%2fhow-to-set-a-variable-in-one-class-and-get-it-in-another%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

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues