Can a singleton in spring have static methods?












2















Component and Service type beans and most of the other bean types are by default singleton.



In one of my code Pull requests, I declared a method as static in a Component as the method wasn't modifying any class level variables.



My code reviewer pointed out that since the class is anyways a Singleton and is going to have a single reference, he said it was unnecessary to make the method static, but rather make it a instance method.



Which way is the correct way of writing code as most of the classes in my application are Singletons?










share|improve this question




















  • 1





    you probably meant not public, but an instance method; after all you can still have public static... methods

    – Eugene
    2 days ago






  • 1





    If you want to use a static util method, then just do that. A singleton's use case is for having a single instance of an object, with functionality exposed through its instance (not static) methods.

    – Tim Biegeleisen
    2 days ago











  • @Eugene thanks. corrected.

    – Vineeth Chitteti
    2 days ago
















2















Component and Service type beans and most of the other bean types are by default singleton.



In one of my code Pull requests, I declared a method as static in a Component as the method wasn't modifying any class level variables.



My code reviewer pointed out that since the class is anyways a Singleton and is going to have a single reference, he said it was unnecessary to make the method static, but rather make it a instance method.



Which way is the correct way of writing code as most of the classes in my application are Singletons?










share|improve this question




















  • 1





    you probably meant not public, but an instance method; after all you can still have public static... methods

    – Eugene
    2 days ago






  • 1





    If you want to use a static util method, then just do that. A singleton's use case is for having a single instance of an object, with functionality exposed through its instance (not static) methods.

    – Tim Biegeleisen
    2 days ago











  • @Eugene thanks. corrected.

    – Vineeth Chitteti
    2 days ago














2












2








2


1






Component and Service type beans and most of the other bean types are by default singleton.



In one of my code Pull requests, I declared a method as static in a Component as the method wasn't modifying any class level variables.



My code reviewer pointed out that since the class is anyways a Singleton and is going to have a single reference, he said it was unnecessary to make the method static, but rather make it a instance method.



Which way is the correct way of writing code as most of the classes in my application are Singletons?










share|improve this question
















Component and Service type beans and most of the other bean types are by default singleton.



In one of my code Pull requests, I declared a method as static in a Component as the method wasn't modifying any class level variables.



My code reviewer pointed out that since the class is anyways a Singleton and is going to have a single reference, he said it was unnecessary to make the method static, but rather make it a instance method.



Which way is the correct way of writing code as most of the classes in my application are Singletons?







java spring oop design-patterns singleton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







Vineeth Chitteti

















asked 2 days ago









Vineeth ChittetiVineeth Chitteti

711522




711522








  • 1





    you probably meant not public, but an instance method; after all you can still have public static... methods

    – Eugene
    2 days ago






  • 1





    If you want to use a static util method, then just do that. A singleton's use case is for having a single instance of an object, with functionality exposed through its instance (not static) methods.

    – Tim Biegeleisen
    2 days ago











  • @Eugene thanks. corrected.

    – Vineeth Chitteti
    2 days ago














  • 1





    you probably meant not public, but an instance method; after all you can still have public static... methods

    – Eugene
    2 days ago






  • 1





    If you want to use a static util method, then just do that. A singleton's use case is for having a single instance of an object, with functionality exposed through its instance (not static) methods.

    – Tim Biegeleisen
    2 days ago











  • @Eugene thanks. corrected.

    – Vineeth Chitteti
    2 days ago








1




1





you probably meant not public, but an instance method; after all you can still have public static... methods

– Eugene
2 days ago





you probably meant not public, but an instance method; after all you can still have public static... methods

– Eugene
2 days ago




1




1





If you want to use a static util method, then just do that. A singleton's use case is for having a single instance of an object, with functionality exposed through its instance (not static) methods.

– Tim Biegeleisen
2 days ago





If you want to use a static util method, then just do that. A singleton's use case is for having a single instance of an object, with functionality exposed through its instance (not static) methods.

– Tim Biegeleisen
2 days ago













@Eugene thanks. corrected.

– Vineeth Chitteti
2 days ago





@Eugene thanks. corrected.

– Vineeth Chitteti
2 days ago












1 Answer
1






active

oldest

votes


















4














Your reviewer is probably right about this one, it makes little sense to declare a static method inside a singleton; unless this static method is a factory method that returns the singleton instance itself.



I could think of two other reasons not to declare static methods in the singleton : testing is harder for static methods in some cases and you will really confuse the caller of this singleton. He/she might see that there is a static method and have a hard time understanding why it was declared like this, it would require extra reasoning as far as I see for such a method.






share|improve this answer



















  • 1





    A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

    – Tim Biegeleisen
    2 days ago






  • 1





    Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

    – Adriaan Koster
    2 days ago











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%2f54252763%2fcan-a-singleton-in-spring-have-static-methods%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









4














Your reviewer is probably right about this one, it makes little sense to declare a static method inside a singleton; unless this static method is a factory method that returns the singleton instance itself.



I could think of two other reasons not to declare static methods in the singleton : testing is harder for static methods in some cases and you will really confuse the caller of this singleton. He/she might see that there is a static method and have a hard time understanding why it was declared like this, it would require extra reasoning as far as I see for such a method.






share|improve this answer



















  • 1





    A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

    – Tim Biegeleisen
    2 days ago






  • 1





    Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

    – Adriaan Koster
    2 days ago
















4














Your reviewer is probably right about this one, it makes little sense to declare a static method inside a singleton; unless this static method is a factory method that returns the singleton instance itself.



I could think of two other reasons not to declare static methods in the singleton : testing is harder for static methods in some cases and you will really confuse the caller of this singleton. He/she might see that there is a static method and have a hard time understanding why it was declared like this, it would require extra reasoning as far as I see for such a method.






share|improve this answer



















  • 1





    A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

    – Tim Biegeleisen
    2 days ago






  • 1





    Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

    – Adriaan Koster
    2 days ago














4












4








4







Your reviewer is probably right about this one, it makes little sense to declare a static method inside a singleton; unless this static method is a factory method that returns the singleton instance itself.



I could think of two other reasons not to declare static methods in the singleton : testing is harder for static methods in some cases and you will really confuse the caller of this singleton. He/she might see that there is a static method and have a hard time understanding why it was declared like this, it would require extra reasoning as far as I see for such a method.






share|improve this answer













Your reviewer is probably right about this one, it makes little sense to declare a static method inside a singleton; unless this static method is a factory method that returns the singleton instance itself.



I could think of two other reasons not to declare static methods in the singleton : testing is harder for static methods in some cases and you will really confuse the caller of this singleton. He/she might see that there is a static method and have a hard time understanding why it was declared like this, it would require extra reasoning as far as I see for such a method.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









EugeneEugene

69.3k999164




69.3k999164








  • 1





    A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

    – Tim Biegeleisen
    2 days ago






  • 1





    Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

    – Adriaan Koster
    2 days ago














  • 1





    A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

    – Tim Biegeleisen
    2 days ago






  • 1





    Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

    – Adriaan Koster
    2 days ago








1




1





A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

– Tim Biegeleisen
2 days ago





A static method is not really associated with the instance, cannot access instance state, and therefore defeats the purpose of using the singleton pattern in the first place.

– Tim Biegeleisen
2 days ago




1




1





Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

– Adriaan Koster
2 days ago





Agreed. My take on this: it's not necessarily 'wrong' but it is unconventional and therefore confusing. Following conventions is an asset for code maintainability.

– Adriaan Koster
2 days ago


















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%2f54252763%2fcan-a-singleton-in-spring-have-static-methods%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

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage