How to set a variable in one class and get it in another
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
|
show 10 more comments
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
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 namedA
with a static setter namedsetA(int a)
and a static gettergetA()
. You can doA.setA(12)
and then from inside another class you can doA.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
|
show 10 more comments
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
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
java variables get set accessor
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 namedA
with a static setter namedsetA(int a)
and a static gettergetA()
. You can doA.setA(12)
and then from inside another class you can doA.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
|
show 10 more comments
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 namedA
with a static setter namedsetA(int a)
and a static gettergetA()
. You can doA.setA(12)
and then from inside another class you can doA.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
|
show 10 more comments
1 Answer
1
active
oldest
votes
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);
}
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%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
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 15 '18 at 21:41
Emmanuel ConradieEmmanuel Conradie
66
66
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%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
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
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 namedsetA(int a)
and a static gettergetA()
. You can doA.setA(12)
and then from inside another class you can doA.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