what is the best way to get other class of object reference??










-2















Sorry, i can't give better title.I have two class A and B.A class is singleton class.it always gives same object to whoever want it.Here B class always want that A object refenence. check follow code



class B

private A a;
B()
this.a=A.getAObject();

public void process(String args)

a.sendData();//line 1
(or)
A.getAObject().sendData();//line 2




which is best way as mentioned above line 1 or line 2 as performance wise like that??










share|improve this question

















  • 1





    line 2. And remove the this.a=A.getAObject(); as class A is singleton object. You always need to access A's object by calling static method!

    – Zico
    Nov 13 '18 at 7:09






  • 1





    This really doesn't make a difference, except perhaps for unit tests or readability.

    – ernest_k
    Nov 13 '18 at 7:09















-2















Sorry, i can't give better title.I have two class A and B.A class is singleton class.it always gives same object to whoever want it.Here B class always want that A object refenence. check follow code



class B

private A a;
B()
this.a=A.getAObject();

public void process(String args)

a.sendData();//line 1
(or)
A.getAObject().sendData();//line 2




which is best way as mentioned above line 1 or line 2 as performance wise like that??










share|improve this question

















  • 1





    line 2. And remove the this.a=A.getAObject(); as class A is singleton object. You always need to access A's object by calling static method!

    – Zico
    Nov 13 '18 at 7:09






  • 1





    This really doesn't make a difference, except perhaps for unit tests or readability.

    – ernest_k
    Nov 13 '18 at 7:09













-2












-2








-2








Sorry, i can't give better title.I have two class A and B.A class is singleton class.it always gives same object to whoever want it.Here B class always want that A object refenence. check follow code



class B

private A a;
B()
this.a=A.getAObject();

public void process(String args)

a.sendData();//line 1
(or)
A.getAObject().sendData();//line 2




which is best way as mentioned above line 1 or line 2 as performance wise like that??










share|improve this question














Sorry, i can't give better title.I have two class A and B.A class is singleton class.it always gives same object to whoever want it.Here B class always want that A object refenence. check follow code



class B

private A a;
B()
this.a=A.getAObject();

public void process(String args)

a.sendData();//line 1
(or)
A.getAObject().sendData();//line 2




which is best way as mentioned above line 1 or line 2 as performance wise like that??







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:05









jagadeesh kalaiyarasanjagadeesh kalaiyarasan

157




157







  • 1





    line 2. And remove the this.a=A.getAObject(); as class A is singleton object. You always need to access A's object by calling static method!

    – Zico
    Nov 13 '18 at 7:09






  • 1





    This really doesn't make a difference, except perhaps for unit tests or readability.

    – ernest_k
    Nov 13 '18 at 7:09












  • 1





    line 2. And remove the this.a=A.getAObject(); as class A is singleton object. You always need to access A's object by calling static method!

    – Zico
    Nov 13 '18 at 7:09






  • 1





    This really doesn't make a difference, except perhaps for unit tests or readability.

    – ernest_k
    Nov 13 '18 at 7:09







1




1





line 2. And remove the this.a=A.getAObject(); as class A is singleton object. You always need to access A's object by calling static method!

– Zico
Nov 13 '18 at 7:09





line 2. And remove the this.a=A.getAObject(); as class A is singleton object. You always need to access A's object by calling static method!

– Zico
Nov 13 '18 at 7:09




1




1





This really doesn't make a difference, except perhaps for unit tests or readability.

– ernest_k
Nov 13 '18 at 7:09





This really doesn't make a difference, except perhaps for unit tests or readability.

– ernest_k
Nov 13 '18 at 7:09












2 Answers
2






active

oldest

votes


















1














You can reference of one class to another class by making reference of the other class



In your code you do something like this :



 class B 
private A a;
B(A a)
this.a=a; //refernce of a

public void display()
a.getA(); //display method of a



class A
public void getA()
....








share|improve this answer




















  • 3





    This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

    – Jon Skeet
    Nov 13 '18 at 7:20


















1














In many cases the differences will be insignificant.



But there would be scenarios like where you have to create a lot of instances of B. So in those scenarios there would be a performance impact since introducing a filed to that class means taking more memory when creating a instance.



If it is not a scenario like that, I think its better to assign it to a variable, since it will improve the readability of the code if you use that instance often in the class.






share|improve this answer




















  • 1





    No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

    – Jon Skeet
    Nov 13 '18 at 7:19












  • @JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

    – Sand
    Nov 13 '18 at 7:30











  • Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

    – Jon Skeet
    Nov 13 '18 at 8:17











  • "Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

    – Sand
    Nov 13 '18 at 8:22











  • @JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

    – Sand
    Nov 13 '18 at 8:46










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%2f53275579%2fwhat-is-the-best-way-to-get-other-class-of-object-reference%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can reference of one class to another class by making reference of the other class



In your code you do something like this :



 class B 
private A a;
B(A a)
this.a=a; //refernce of a

public void display()
a.getA(); //display method of a



class A
public void getA()
....








share|improve this answer




















  • 3





    This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

    – Jon Skeet
    Nov 13 '18 at 7:20















1














You can reference of one class to another class by making reference of the other class



In your code you do something like this :



 class B 
private A a;
B(A a)
this.a=a; //refernce of a

public void display()
a.getA(); //display method of a



class A
public void getA()
....








share|improve this answer




















  • 3





    This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

    – Jon Skeet
    Nov 13 '18 at 7:20













1












1








1







You can reference of one class to another class by making reference of the other class



In your code you do something like this :



 class B 
private A a;
B(A a)
this.a=a; //refernce of a

public void display()
a.getA(); //display method of a



class A
public void getA()
....








share|improve this answer















You can reference of one class to another class by making reference of the other class



In your code you do something like this :



 class B 
private A a;
B(A a)
this.a=a; //refernce of a

public void display()
a.getA(); //display method of a



class A
public void getA()
....









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 7:22

























answered Nov 13 '18 at 7:17









SwatiSwati

266




266







  • 3





    This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

    – Jon Skeet
    Nov 13 '18 at 7:20












  • 3





    This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

    – Jon Skeet
    Nov 13 '18 at 7:20







3




3





This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

– Jon Skeet
Nov 13 '18 at 7:20





This doesn't look like it's answering the question asked. The OP wants to get at the singleton A from B, not vice versa.

– Jon Skeet
Nov 13 '18 at 7:20













1














In many cases the differences will be insignificant.



But there would be scenarios like where you have to create a lot of instances of B. So in those scenarios there would be a performance impact since introducing a filed to that class means taking more memory when creating a instance.



If it is not a scenario like that, I think its better to assign it to a variable, since it will improve the readability of the code if you use that instance often in the class.






share|improve this answer




















  • 1





    No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

    – Jon Skeet
    Nov 13 '18 at 7:19












  • @JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

    – Sand
    Nov 13 '18 at 7:30











  • Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

    – Jon Skeet
    Nov 13 '18 at 8:17











  • "Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

    – Sand
    Nov 13 '18 at 8:22











  • @JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

    – Sand
    Nov 13 '18 at 8:46















1














In many cases the differences will be insignificant.



But there would be scenarios like where you have to create a lot of instances of B. So in those scenarios there would be a performance impact since introducing a filed to that class means taking more memory when creating a instance.



If it is not a scenario like that, I think its better to assign it to a variable, since it will improve the readability of the code if you use that instance often in the class.






share|improve this answer




















  • 1





    No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

    – Jon Skeet
    Nov 13 '18 at 7:19












  • @JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

    – Sand
    Nov 13 '18 at 7:30











  • Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

    – Jon Skeet
    Nov 13 '18 at 8:17











  • "Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

    – Sand
    Nov 13 '18 at 8:22











  • @JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

    – Sand
    Nov 13 '18 at 8:46













1












1








1







In many cases the differences will be insignificant.



But there would be scenarios like where you have to create a lot of instances of B. So in those scenarios there would be a performance impact since introducing a filed to that class means taking more memory when creating a instance.



If it is not a scenario like that, I think its better to assign it to a variable, since it will improve the readability of the code if you use that instance often in the class.






share|improve this answer















In many cases the differences will be insignificant.



But there would be scenarios like where you have to create a lot of instances of B. So in those scenarios there would be a performance impact since introducing a filed to that class means taking more memory when creating a instance.



If it is not a scenario like that, I think its better to assign it to a variable, since it will improve the readability of the code if you use that instance often in the class.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 9:40

























answered Nov 13 '18 at 7:11









SandSand

1,425319




1,425319







  • 1





    No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

    – Jon Skeet
    Nov 13 '18 at 7:19












  • @JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

    – Sand
    Nov 13 '18 at 7:30











  • Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

    – Jon Skeet
    Nov 13 '18 at 8:17











  • "Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

    – Sand
    Nov 13 '18 at 8:22











  • @JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

    – Sand
    Nov 13 '18 at 8:46












  • 1





    No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

    – Jon Skeet
    Nov 13 '18 at 7:19












  • @JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

    – Sand
    Nov 13 '18 at 7:30











  • Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

    – Jon Skeet
    Nov 13 '18 at 8:17











  • "Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

    – Sand
    Nov 13 '18 at 8:22











  • @JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

    – Sand
    Nov 13 '18 at 8:46







1




1





No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

– Jon Skeet
Nov 13 '18 at 7:19






No, introducing a field in class B means that every instance will take more memory. If there are lots of instances of B, that could have a very serious impact. There's really no point in having a copy of the same value in every object of type B.

– Jon Skeet
Nov 13 '18 at 7:19














@JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

– Sand
Nov 13 '18 at 7:30





@JonSkeet He is asking about performance wise. And you are answering about a specific scenario where a lot of Objects of B can created. I was answering generally :)

– Sand
Nov 13 '18 at 7:30













Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

– Jon Skeet
Nov 13 '18 at 8:17





Memory is part of performance - even in terms of execution time, as the more memory that is taken, the less effective the memory caches will be. I fail to see how "if you have to use the methods and variables in A class a lot" is general but "if there are lots of instances of B" is specific. Besides, making a general claim for something that fails to include a specific but entirely reasonable scenario isn't great either. The performance of the two options simply isn't the same.

– Jon Skeet
Nov 13 '18 at 8:17













"Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

– Sand
Nov 13 '18 at 8:22





"Both will be same is the general" part. and the difference between the part you highlighted in mine and yours is "if". performance of the two options simply isn't the same for your scenario. True

– Sand
Nov 13 '18 at 8:22













@JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

– Sand
Nov 13 '18 at 8:46





@JonSkeet Since I can't edit the last comment, You are saying No, based on your scenario and I'm not saying "Both will be same" based on my scenario you have highlighted. I have mentioned my scenario to just give an extra info about the readability :)

– Sand
Nov 13 '18 at 8:46

















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%2f53275579%2fwhat-is-the-best-way-to-get-other-class-of-object-reference%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto