Java 9 replace Class.newInstance











up vote
13
down vote

favorite
4












Class.newInstance was deprecated in Java 9:




clazz.newInstance()


can be replaced by



clazz.getDeclaredConstructor().newInstance()



The problem is that getDeclaredConstructor returns any constructor without regarding the access level.



If I want to replace all occurrences in my code (on different packages/access level) should I use getConstructor to get the public constructor?




the Constructor object of the public constructor that matches the specified parameterTypes




Or can't I bulk replace all occurrences because it needs to be per case (if a public constructor exists and/or if I have the right access level for the class)?



EDIT



getDeclaredConstructor:




   return getConstructor0(parameterTypes, Member.DECLARED);



getConstructor:




   return getConstructor0(parameterTypes, Member.PUBLIC);










share|improve this question




















  • 1




    If you need to access constructor at different levels, getDeclaredConstructor(s) is the only way out. Can't you use getDeclaredConstructors​() (not getDeclaredConstructor()) to get all constructors and replace one depending on your logic?
    – Sukhpal Singh
    Nov 12 at 7:02






  • 5




    As the deprecation message says, the equivalent code to clazz.newInstance() is clazz.getDeclaredConstructor().newInstance(). I would assume this means that anywhere you're successfully using clazz.newInstance() can be replaced with the recommended code. If you replace clazz.newInstance() with clazz.getConstructor().newInstance() you're changing the semantics of the code.
    – Slaw
    Nov 12 at 7:04








  • 2




    I haven’t tried, but I wouldn’t expect clazz.newInstance() to work either if the zero-arg constructor isn’t public — does it?? So what would you be losing in practice by using clazz.getDeclaredConstructor().newInstance() instead?
    – Ole V.V.
    Nov 12 at 7:22






  • 2




    @OleV.V. Yes, Class.newInstance() works with non-public constructors. But it requires you to have the proper access else it throws an IllegalAccessException (from some quick, non-exhaustive testing).
    – Slaw
    Nov 12 at 7:28








  • 5




    Related (on the reasons why it was deprecated): stackoverflow.com/questions/195321/…
    – Hulk
    Nov 12 at 8:09















up vote
13
down vote

favorite
4












Class.newInstance was deprecated in Java 9:




clazz.newInstance()


can be replaced by



clazz.getDeclaredConstructor().newInstance()



The problem is that getDeclaredConstructor returns any constructor without regarding the access level.



If I want to replace all occurrences in my code (on different packages/access level) should I use getConstructor to get the public constructor?




the Constructor object of the public constructor that matches the specified parameterTypes




Or can't I bulk replace all occurrences because it needs to be per case (if a public constructor exists and/or if I have the right access level for the class)?



EDIT



getDeclaredConstructor:




   return getConstructor0(parameterTypes, Member.DECLARED);



getConstructor:




   return getConstructor0(parameterTypes, Member.PUBLIC);










share|improve this question




















  • 1




    If you need to access constructor at different levels, getDeclaredConstructor(s) is the only way out. Can't you use getDeclaredConstructors​() (not getDeclaredConstructor()) to get all constructors and replace one depending on your logic?
    – Sukhpal Singh
    Nov 12 at 7:02






  • 5




    As the deprecation message says, the equivalent code to clazz.newInstance() is clazz.getDeclaredConstructor().newInstance(). I would assume this means that anywhere you're successfully using clazz.newInstance() can be replaced with the recommended code. If you replace clazz.newInstance() with clazz.getConstructor().newInstance() you're changing the semantics of the code.
    – Slaw
    Nov 12 at 7:04








  • 2




    I haven’t tried, but I wouldn’t expect clazz.newInstance() to work either if the zero-arg constructor isn’t public — does it?? So what would you be losing in practice by using clazz.getDeclaredConstructor().newInstance() instead?
    – Ole V.V.
    Nov 12 at 7:22






  • 2




    @OleV.V. Yes, Class.newInstance() works with non-public constructors. But it requires you to have the proper access else it throws an IllegalAccessException (from some quick, non-exhaustive testing).
    – Slaw
    Nov 12 at 7:28








  • 5




    Related (on the reasons why it was deprecated): stackoverflow.com/questions/195321/…
    – Hulk
    Nov 12 at 8:09













up vote
13
down vote

favorite
4









up vote
13
down vote

favorite
4






4





Class.newInstance was deprecated in Java 9:




clazz.newInstance()


can be replaced by



clazz.getDeclaredConstructor().newInstance()



The problem is that getDeclaredConstructor returns any constructor without regarding the access level.



If I want to replace all occurrences in my code (on different packages/access level) should I use getConstructor to get the public constructor?




the Constructor object of the public constructor that matches the specified parameterTypes




Or can't I bulk replace all occurrences because it needs to be per case (if a public constructor exists and/or if I have the right access level for the class)?



EDIT



getDeclaredConstructor:




   return getConstructor0(parameterTypes, Member.DECLARED);



getConstructor:




   return getConstructor0(parameterTypes, Member.PUBLIC);










share|improve this question















Class.newInstance was deprecated in Java 9:




clazz.newInstance()


can be replaced by



clazz.getDeclaredConstructor().newInstance()



The problem is that getDeclaredConstructor returns any constructor without regarding the access level.



If I want to replace all occurrences in my code (on different packages/access level) should I use getConstructor to get the public constructor?




the Constructor object of the public constructor that matches the specified parameterTypes




Or can't I bulk replace all occurrences because it needs to be per case (if a public constructor exists and/or if I have the right access level for the class)?



EDIT



getDeclaredConstructor:




   return getConstructor0(parameterTypes, Member.DECLARED);



getConstructor:




   return getConstructor0(parameterTypes, Member.PUBLIC);







java reflection constructor java-9






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 14:33









Holger

161k23223428




161k23223428










asked Nov 12 at 6:44









user7294900

19.8k93257




19.8k93257








  • 1




    If you need to access constructor at different levels, getDeclaredConstructor(s) is the only way out. Can't you use getDeclaredConstructors​() (not getDeclaredConstructor()) to get all constructors and replace one depending on your logic?
    – Sukhpal Singh
    Nov 12 at 7:02






  • 5




    As the deprecation message says, the equivalent code to clazz.newInstance() is clazz.getDeclaredConstructor().newInstance(). I would assume this means that anywhere you're successfully using clazz.newInstance() can be replaced with the recommended code. If you replace clazz.newInstance() with clazz.getConstructor().newInstance() you're changing the semantics of the code.
    – Slaw
    Nov 12 at 7:04








  • 2




    I haven’t tried, but I wouldn’t expect clazz.newInstance() to work either if the zero-arg constructor isn’t public — does it?? So what would you be losing in practice by using clazz.getDeclaredConstructor().newInstance() instead?
    – Ole V.V.
    Nov 12 at 7:22






  • 2




    @OleV.V. Yes, Class.newInstance() works with non-public constructors. But it requires you to have the proper access else it throws an IllegalAccessException (from some quick, non-exhaustive testing).
    – Slaw
    Nov 12 at 7:28








  • 5




    Related (on the reasons why it was deprecated): stackoverflow.com/questions/195321/…
    – Hulk
    Nov 12 at 8:09














  • 1




    If you need to access constructor at different levels, getDeclaredConstructor(s) is the only way out. Can't you use getDeclaredConstructors​() (not getDeclaredConstructor()) to get all constructors and replace one depending on your logic?
    – Sukhpal Singh
    Nov 12 at 7:02






  • 5




    As the deprecation message says, the equivalent code to clazz.newInstance() is clazz.getDeclaredConstructor().newInstance(). I would assume this means that anywhere you're successfully using clazz.newInstance() can be replaced with the recommended code. If you replace clazz.newInstance() with clazz.getConstructor().newInstance() you're changing the semantics of the code.
    – Slaw
    Nov 12 at 7:04








  • 2




    I haven’t tried, but I wouldn’t expect clazz.newInstance() to work either if the zero-arg constructor isn’t public — does it?? So what would you be losing in practice by using clazz.getDeclaredConstructor().newInstance() instead?
    – Ole V.V.
    Nov 12 at 7:22






  • 2




    @OleV.V. Yes, Class.newInstance() works with non-public constructors. But it requires you to have the proper access else it throws an IllegalAccessException (from some quick, non-exhaustive testing).
    – Slaw
    Nov 12 at 7:28








  • 5




    Related (on the reasons why it was deprecated): stackoverflow.com/questions/195321/…
    – Hulk
    Nov 12 at 8:09








1




1




If you need to access constructor at different levels, getDeclaredConstructor(s) is the only way out. Can't you use getDeclaredConstructors​() (not getDeclaredConstructor()) to get all constructors and replace one depending on your logic?
– Sukhpal Singh
Nov 12 at 7:02




If you need to access constructor at different levels, getDeclaredConstructor(s) is the only way out. Can't you use getDeclaredConstructors​() (not getDeclaredConstructor()) to get all constructors and replace one depending on your logic?
– Sukhpal Singh
Nov 12 at 7:02




5




5




As the deprecation message says, the equivalent code to clazz.newInstance() is clazz.getDeclaredConstructor().newInstance(). I would assume this means that anywhere you're successfully using clazz.newInstance() can be replaced with the recommended code. If you replace clazz.newInstance() with clazz.getConstructor().newInstance() you're changing the semantics of the code.
– Slaw
Nov 12 at 7:04






As the deprecation message says, the equivalent code to clazz.newInstance() is clazz.getDeclaredConstructor().newInstance(). I would assume this means that anywhere you're successfully using clazz.newInstance() can be replaced with the recommended code. If you replace clazz.newInstance() with clazz.getConstructor().newInstance() you're changing the semantics of the code.
– Slaw
Nov 12 at 7:04






2




2




I haven’t tried, but I wouldn’t expect clazz.newInstance() to work either if the zero-arg constructor isn’t public — does it?? So what would you be losing in practice by using clazz.getDeclaredConstructor().newInstance() instead?
– Ole V.V.
Nov 12 at 7:22




I haven’t tried, but I wouldn’t expect clazz.newInstance() to work either if the zero-arg constructor isn’t public — does it?? So what would you be losing in practice by using clazz.getDeclaredConstructor().newInstance() instead?
– Ole V.V.
Nov 12 at 7:22




2




2




@OleV.V. Yes, Class.newInstance() works with non-public constructors. But it requires you to have the proper access else it throws an IllegalAccessException (from some quick, non-exhaustive testing).
– Slaw
Nov 12 at 7:28






@OleV.V. Yes, Class.newInstance() works with non-public constructors. But it requires you to have the proper access else it throws an IllegalAccessException (from some quick, non-exhaustive testing).
– Slaw
Nov 12 at 7:28






5




5




Related (on the reasons why it was deprecated): stackoverflow.com/questions/195321/…
– Hulk
Nov 12 at 8:09




Related (on the reasons why it was deprecated): stackoverflow.com/questions/195321/…
– Hulk
Nov 12 at 8:09












1 Answer
1






active

oldest

votes

















up vote
8
down vote



accepted










Class.newInstance() invokes the zero-argument constructor, whether it is public or not. It performs a runtime check of the caller's access to that constructor.



Calling getDeclaredConstructor() returns that same constructor. Calling getDeclaredConstructor().newInstance() performs the same runtime checks. Except for the different handling of exceptions, it does the same thing.



No, don't change it to getConstructor(). That would cause a NoSuchMethodException for non-public constructors.






share|improve this answer





















  • Thank you, so any exceptions thrown will only be a valid initialization failures?
    – user7294900
    Nov 12 at 17:03












  • @user7294900 Yes.
    – Boann
    Nov 12 at 22:22











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',
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%2f53257073%2fjava-9-replace-class-newinstance%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








up vote
8
down vote



accepted










Class.newInstance() invokes the zero-argument constructor, whether it is public or not. It performs a runtime check of the caller's access to that constructor.



Calling getDeclaredConstructor() returns that same constructor. Calling getDeclaredConstructor().newInstance() performs the same runtime checks. Except for the different handling of exceptions, it does the same thing.



No, don't change it to getConstructor(). That would cause a NoSuchMethodException for non-public constructors.






share|improve this answer





















  • Thank you, so any exceptions thrown will only be a valid initialization failures?
    – user7294900
    Nov 12 at 17:03












  • @user7294900 Yes.
    – Boann
    Nov 12 at 22:22















up vote
8
down vote



accepted










Class.newInstance() invokes the zero-argument constructor, whether it is public or not. It performs a runtime check of the caller's access to that constructor.



Calling getDeclaredConstructor() returns that same constructor. Calling getDeclaredConstructor().newInstance() performs the same runtime checks. Except for the different handling of exceptions, it does the same thing.



No, don't change it to getConstructor(). That would cause a NoSuchMethodException for non-public constructors.






share|improve this answer





















  • Thank you, so any exceptions thrown will only be a valid initialization failures?
    – user7294900
    Nov 12 at 17:03












  • @user7294900 Yes.
    – Boann
    Nov 12 at 22:22













up vote
8
down vote



accepted







up vote
8
down vote



accepted






Class.newInstance() invokes the zero-argument constructor, whether it is public or not. It performs a runtime check of the caller's access to that constructor.



Calling getDeclaredConstructor() returns that same constructor. Calling getDeclaredConstructor().newInstance() performs the same runtime checks. Except for the different handling of exceptions, it does the same thing.



No, don't change it to getConstructor(). That would cause a NoSuchMethodException for non-public constructors.






share|improve this answer












Class.newInstance() invokes the zero-argument constructor, whether it is public or not. It performs a runtime check of the caller's access to that constructor.



Calling getDeclaredConstructor() returns that same constructor. Calling getDeclaredConstructor().newInstance() performs the same runtime checks. Except for the different handling of exceptions, it does the same thing.



No, don't change it to getConstructor(). That would cause a NoSuchMethodException for non-public constructors.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:22









Boann

36.6k1287121




36.6k1287121












  • Thank you, so any exceptions thrown will only be a valid initialization failures?
    – user7294900
    Nov 12 at 17:03












  • @user7294900 Yes.
    – Boann
    Nov 12 at 22:22


















  • Thank you, so any exceptions thrown will only be a valid initialization failures?
    – user7294900
    Nov 12 at 17:03












  • @user7294900 Yes.
    – Boann
    Nov 12 at 22:22
















Thank you, so any exceptions thrown will only be a valid initialization failures?
– user7294900
Nov 12 at 17:03






Thank you, so any exceptions thrown will only be a valid initialization failures?
– user7294900
Nov 12 at 17:03














@user7294900 Yes.
– Boann
Nov 12 at 22:22




@user7294900 Yes.
– Boann
Nov 12 at 22:22


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53257073%2fjava-9-replace-class-newinstance%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python