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?
java constructor jersey pojo moxy
|
show 1 more comment
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?
java constructor jersey pojo moxy
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
|
show 1 more comment
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?
java constructor jersey pojo moxy
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
java constructor jersey pojo moxy
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
|
show 1 more comment
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
|
show 1 more comment
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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%2f53194025%2fhow-to-handle-empty-constructor-with-final-fields%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
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