How to handle empty constructor with final fields











up vote
0
down vote

favorite












I have a class like this



 public class Test {

private String m_username;

public Test() {}

public Test(String username) {
m_username = username;
}
}


And with Moxy. I can post this POJO to other API using Jersey client without any converting operation. But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username. And also the Moxy doesn't work. How can I fix that?










share|improve this question






















  • I downvoted because "it's not working" is not helpful
    – Andreas
    Nov 7 at 17:01






  • 1




    "But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username" So what's stopping you from doing that?
    – Andreas
    Nov 7 at 17:01










  • @HenlenLee please provide error trace of what you are getting.
    – Jishnu Prathap
    Nov 7 at 17:29










  • @Andreas I need to keep m_username final, but I also need an empty constructor to let Moxy work. That's the problem I try to figure out.
    – HenlenLee
    Nov 7 at 18:02










  • Use Jackson instead with the @JsonCreator
    – Paul Samsotha
    Nov 8 at 16:45















up vote
0
down vote

favorite












I have a class like this



 public class Test {

private String m_username;

public Test() {}

public Test(String username) {
m_username = username;
}
}


And with Moxy. I can post this POJO to other API using Jersey client without any converting operation. But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username. And also the Moxy doesn't work. How can I fix that?










share|improve this question






















  • I downvoted because "it's not working" is not helpful
    – Andreas
    Nov 7 at 17:01






  • 1




    "But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username" So what's stopping you from doing that?
    – Andreas
    Nov 7 at 17:01










  • @HenlenLee please provide error trace of what you are getting.
    – Jishnu Prathap
    Nov 7 at 17:29










  • @Andreas I need to keep m_username final, but I also need an empty constructor to let Moxy work. That's the problem I try to figure out.
    – HenlenLee
    Nov 7 at 18:02










  • Use Jackson instead with the @JsonCreator
    – Paul Samsotha
    Nov 8 at 16:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a class like this



 public class Test {

private String m_username;

public Test() {}

public Test(String username) {
m_username = username;
}
}


And with Moxy. I can post this POJO to other API using Jersey client without any converting operation. But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username. And also the Moxy doesn't work. How can I fix that?










share|improve this question













I have a class like this



 public class Test {

private String m_username;

public Test() {}

public Test(String username) {
m_username = username;
}
}


And with Moxy. I can post this POJO to other API using Jersey client without any converting operation. But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username. And also the Moxy doesn't work. How can I fix that?







java constructor jersey pojo moxy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 16:44









HenlenLee

697




697












  • I downvoted because "it's not working" is not helpful
    – Andreas
    Nov 7 at 17:01






  • 1




    "But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username" So what's stopping you from doing that?
    – Andreas
    Nov 7 at 17:01










  • @HenlenLee please provide error trace of what you are getting.
    – Jishnu Prathap
    Nov 7 at 17:29










  • @Andreas I need to keep m_username final, but I also need an empty constructor to let Moxy work. That's the problem I try to figure out.
    – HenlenLee
    Nov 7 at 18:02










  • Use Jackson instead with the @JsonCreator
    – Paul Samsotha
    Nov 8 at 16:45


















  • I downvoted because "it's not working" is not helpful
    – Andreas
    Nov 7 at 17:01






  • 1




    "But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username" So what's stopping you from doing that?
    – Andreas
    Nov 7 at 17:01










  • @HenlenLee please provide error trace of what you are getting.
    – Jishnu Prathap
    Nov 7 at 17:29










  • @Andreas I need to keep m_username final, but I also need an empty constructor to let Moxy work. That's the problem I try to figure out.
    – HenlenLee
    Nov 7 at 18:02










  • Use Jackson instead with the @JsonCreator
    – Paul Samsotha
    Nov 8 at 16:45
















I downvoted because "it's not working" is not helpful
– Andreas
Nov 7 at 17:01




I downvoted because "it's not working" is not helpful
– Andreas
Nov 7 at 17:01




1




1




"But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username" So what's stopping you from doing that?
– Andreas
Nov 7 at 17:01




"But I need to set the m_username as a final field and that will need the empty constructor to initiate m_username" So what's stopping you from doing that?
– Andreas
Nov 7 at 17:01












@HenlenLee please provide error trace of what you are getting.
– Jishnu Prathap
Nov 7 at 17:29




@HenlenLee please provide error trace of what you are getting.
– Jishnu Prathap
Nov 7 at 17:29












@Andreas I need to keep m_username final, but I also need an empty constructor to let Moxy work. That's the problem I try to figure out.
– HenlenLee
Nov 7 at 18:02




@Andreas I need to keep m_username final, but I also need an empty constructor to let Moxy work. That's the problem I try to figure out.
– HenlenLee
Nov 7 at 18:02












Use Jackson instead with the @JsonCreator
– Paul Samsotha
Nov 8 at 16:45




Use Jackson instead with the @JsonCreator
– Paul Samsotha
Nov 8 at 16:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The question isn't very well asked.
AS far as I understand:




  • You have to make your field final

  • You have to keep the empty constructor because your object is automatically serialized/deserialized in a format like JSON, using a library such as those you can find in Spring


Unfortunately, these two constraints can't be held at the same time. You will need to abandon final if you want to keep the empty constructor, and conversely.






share|improve this answer





















  • Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
    – HenlenLee
    Nov 7 at 17:55










  • That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
    – QuentinC
    Nov 7 at 21:51











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%2f53194025%2fhow-to-handle-empty-constructor-with-final-fields%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
0
down vote













The question isn't very well asked.
AS far as I understand:




  • You have to make your field final

  • You have to keep the empty constructor because your object is automatically serialized/deserialized in a format like JSON, using a library such as those you can find in Spring


Unfortunately, these two constraints can't be held at the same time. You will need to abandon final if you want to keep the empty constructor, and conversely.






share|improve this answer





















  • Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
    – HenlenLee
    Nov 7 at 17:55










  • That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
    – QuentinC
    Nov 7 at 21:51















up vote
0
down vote













The question isn't very well asked.
AS far as I understand:




  • You have to make your field final

  • You have to keep the empty constructor because your object is automatically serialized/deserialized in a format like JSON, using a library such as those you can find in Spring


Unfortunately, these two constraints can't be held at the same time. You will need to abandon final if you want to keep the empty constructor, and conversely.






share|improve this answer





















  • Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
    – HenlenLee
    Nov 7 at 17:55










  • That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
    – QuentinC
    Nov 7 at 21:51













up vote
0
down vote










up vote
0
down vote









The question isn't very well asked.
AS far as I understand:




  • You have to make your field final

  • You have to keep the empty constructor because your object is automatically serialized/deserialized in a format like JSON, using a library such as those you can find in Spring


Unfortunately, these two constraints can't be held at the same time. You will need to abandon final if you want to keep the empty constructor, and conversely.






share|improve this answer












The question isn't very well asked.
AS far as I understand:




  • You have to make your field final

  • You have to keep the empty constructor because your object is automatically serialized/deserialized in a format like JSON, using a library such as those you can find in Spring


Unfortunately, these two constraints can't be held at the same time. You will need to abandon final if you want to keep the empty constructor, and conversely.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 17:40









QuentinC

2,23031018




2,23031018












  • Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
    – HenlenLee
    Nov 7 at 17:55










  • That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
    – QuentinC
    Nov 7 at 21:51


















  • Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
    – HenlenLee
    Nov 7 at 17:55










  • That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
    – QuentinC
    Nov 7 at 21:51
















Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
– HenlenLee
Nov 7 at 17:55




Yes. That's my question. It seems like I have to use another library to convert the POJO if the m_username is final
– HenlenLee
Nov 7 at 17:55












That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
– QuentinC
Nov 7 at 21:51




That's exact. POJOs that need to be automatically serialized/deserialized can't have final fields because they require your class to have a no-args constructor, as I have explained in my answer. You can't have both.
– QuentinC
Nov 7 at 21:51


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194025%2fhow-to-handle-empty-constructor-with-final-fields%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini