How to make limitations for constructor values in java? [closed]
up vote
-4
down vote
favorite
What is the right way in java to limit constructor values?
I want to make sure that its NOT possible to create objects using MyConstructor with the parameter int value > 5
example: (pseudo code)
public MyConstructor(value){
if(value < 5){
this.value = value;
}
}
java
closed as unclear what you're asking by JB Nizet, Andrew Tobilko, Roshana Pitigala, GhostCat, gnat Nov 11 at 17:59
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
up vote
-4
down vote
favorite
What is the right way in java to limit constructor values?
I want to make sure that its NOT possible to create objects using MyConstructor with the parameter int value > 5
example: (pseudo code)
public MyConstructor(value){
if(value < 5){
this.value = value;
}
}
java
closed as unclear what you're asking by JB Nizet, Andrew Tobilko, Roshana Pitigala, GhostCat, gnat Nov 11 at 17:59
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
1: that code won't compile 2: use an enum for compile-time safety
– Hovercraft Full Of Eels
Nov 11 at 13:51
1
Although the code is not valid Java code, I understand your question. An enum or a specialized type may help. Also, do you want the limitation at compile time or run time?
– Sid
Nov 11 at 13:52
3
Explain precisely what you actually want to achieve. What is the actual class, what is the actual parameter for, what does it represent, and why it must be less than 5. At the moment, your question is much too vague to provide good advice.
– JB Nizet
Nov 11 at 13:54
2
we normally throw anIllegalArgumentException
ifvalue >= 5
– Andrew Tobilko
Nov 11 at 14:05
You could throw IllegalArgumentException when an invalid value is passed.
– D.B.
Nov 11 at 14:06
|
show 2 more comments
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
What is the right way in java to limit constructor values?
I want to make sure that its NOT possible to create objects using MyConstructor with the parameter int value > 5
example: (pseudo code)
public MyConstructor(value){
if(value < 5){
this.value = value;
}
}
java
What is the right way in java to limit constructor values?
I want to make sure that its NOT possible to create objects using MyConstructor with the parameter int value > 5
example: (pseudo code)
public MyConstructor(value){
if(value < 5){
this.value = value;
}
}
java
java
edited Nov 15 at 10:10
asked Nov 11 at 13:50
QickdmG dombu
506
506
closed as unclear what you're asking by JB Nizet, Andrew Tobilko, Roshana Pitigala, GhostCat, gnat Nov 11 at 17:59
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by JB Nizet, Andrew Tobilko, Roshana Pitigala, GhostCat, gnat Nov 11 at 17:59
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
1: that code won't compile 2: use an enum for compile-time safety
– Hovercraft Full Of Eels
Nov 11 at 13:51
1
Although the code is not valid Java code, I understand your question. An enum or a specialized type may help. Also, do you want the limitation at compile time or run time?
– Sid
Nov 11 at 13:52
3
Explain precisely what you actually want to achieve. What is the actual class, what is the actual parameter for, what does it represent, and why it must be less than 5. At the moment, your question is much too vague to provide good advice.
– JB Nizet
Nov 11 at 13:54
2
we normally throw anIllegalArgumentException
ifvalue >= 5
– Andrew Tobilko
Nov 11 at 14:05
You could throw IllegalArgumentException when an invalid value is passed.
– D.B.
Nov 11 at 14:06
|
show 2 more comments
5
1: that code won't compile 2: use an enum for compile-time safety
– Hovercraft Full Of Eels
Nov 11 at 13:51
1
Although the code is not valid Java code, I understand your question. An enum or a specialized type may help. Also, do you want the limitation at compile time or run time?
– Sid
Nov 11 at 13:52
3
Explain precisely what you actually want to achieve. What is the actual class, what is the actual parameter for, what does it represent, and why it must be less than 5. At the moment, your question is much too vague to provide good advice.
– JB Nizet
Nov 11 at 13:54
2
we normally throw anIllegalArgumentException
ifvalue >= 5
– Andrew Tobilko
Nov 11 at 14:05
You could throw IllegalArgumentException when an invalid value is passed.
– D.B.
Nov 11 at 14:06
5
5
1: that code won't compile 2: use an enum for compile-time safety
– Hovercraft Full Of Eels
Nov 11 at 13:51
1: that code won't compile 2: use an enum for compile-time safety
– Hovercraft Full Of Eels
Nov 11 at 13:51
1
1
Although the code is not valid Java code, I understand your question. An enum or a specialized type may help. Also, do you want the limitation at compile time or run time?
– Sid
Nov 11 at 13:52
Although the code is not valid Java code, I understand your question. An enum or a specialized type may help. Also, do you want the limitation at compile time or run time?
– Sid
Nov 11 at 13:52
3
3
Explain precisely what you actually want to achieve. What is the actual class, what is the actual parameter for, what does it represent, and why it must be less than 5. At the moment, your question is much too vague to provide good advice.
– JB Nizet
Nov 11 at 13:54
Explain precisely what you actually want to achieve. What is the actual class, what is the actual parameter for, what does it represent, and why it must be less than 5. At the moment, your question is much too vague to provide good advice.
– JB Nizet
Nov 11 at 13:54
2
2
we normally throw an
IllegalArgumentException
if value >= 5
– Andrew Tobilko
Nov 11 at 14:05
we normally throw an
IllegalArgumentException
if value >= 5
– Andrew Tobilko
Nov 11 at 14:05
You could throw IllegalArgumentException when an invalid value is passed.
– D.B.
Nov 11 at 14:06
You could throw IllegalArgumentException when an invalid value is passed.
– D.B.
Nov 11 at 14:06
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
You can use JSR-303 Bean Validation API:
@Max(4)
int value
JSR-303 allows you to define declarative validation constraints against such properties:
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
Specifically @Max
The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10)
int quantity;
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can use JSR-303 Bean Validation API:
@Max(4)
int value
JSR-303 allows you to define declarative validation constraints against such properties:
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
Specifically @Max
The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10)
int quantity;
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
add a comment |
up vote
2
down vote
You can use JSR-303 Bean Validation API:
@Max(4)
int value
JSR-303 allows you to define declarative validation constraints against such properties:
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
Specifically @Max
The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10)
int quantity;
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
add a comment |
up vote
2
down vote
up vote
2
down vote
You can use JSR-303 Bean Validation API:
@Max(4)
int value
JSR-303 allows you to define declarative validation constraints against such properties:
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
Specifically @Max
The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10)
int quantity;
You can use JSR-303 Bean Validation API:
@Max(4)
int value
JSR-303 allows you to define declarative validation constraints against such properties:
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
Specifically @Max
The value of the field or property must be an integer value lower than or equal to the number in the value element.
@Max(10)
int quantity;
edited Nov 11 at 14:02
answered Nov 11 at 13:56
user7294900
19.6k93157
19.6k93157
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
add a comment |
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
That does not prevent you from constructing such objects though, I don't think that's what OP means. (plus it's a bit convoluted for doing such a basic thing)
– Joeri Hendrickx
Nov 15 at 15:27
add a comment |
5
1: that code won't compile 2: use an enum for compile-time safety
– Hovercraft Full Of Eels
Nov 11 at 13:51
1
Although the code is not valid Java code, I understand your question. An enum or a specialized type may help. Also, do you want the limitation at compile time or run time?
– Sid
Nov 11 at 13:52
3
Explain precisely what you actually want to achieve. What is the actual class, what is the actual parameter for, what does it represent, and why it must be less than 5. At the moment, your question is much too vague to provide good advice.
– JB Nizet
Nov 11 at 13:54
2
we normally throw an
IllegalArgumentException
ifvalue >= 5
– Andrew Tobilko
Nov 11 at 14:05
You could throw IllegalArgumentException when an invalid value is passed.
– D.B.
Nov 11 at 14:06