public Tipo getTipo() {
return this.Importo < 0.0 ? Tipo.USCITA : Tipo.ENTRATA;
}
public int compareTo(Movimento m) {
if (this.idConto != this.idConto) {
return this.idConto - this.idConto;
}
return this.DataMov.compareTo(this.DataMov);
}
public static enum Tipo {
ENTRATA,// here i have this error : The constructor Movimento.Tipo() is undefined
USCITA;// here is the same : The constructor Movimento.Tipo() is undefined
private Tipo(String string2, int n2) {
}
}
I already have constructor that I need, what else I need to write?
I am not sure how do you want to define enum. There are basically 2 solutions to this:
1. Define no parameter enum
public static enum Tipo {
ENTRATA,
USCITA;
}
2. Define enum with parameters
public static enum Tipo {
ENTRATA("Entrata", 1),
USCITA("Uscita", 2);
private String string;
private int integer;
private Tipo(String string, int integer) {
this.string = string;
this.integer = integer;
}
}
You wrote a constructor that takes two arguments, but no default constructor. That means the compiler will not provide a no-arg constructor. You should provide one or remove the private constructor.
I see no reason for the private constructor with two arguments. You don't have any private data members in your enum.
Why is your enum static? Remove that.
public enum Tipo {
ENTRATA, USCITA;
}
You are written wrong enum.
public enum abc {
ENTRATA("abc", 1),// here i have this error : The constructor Movimento.Tipo() is undefined
USCITA("xyz", 2);// here is the same : The constructor Movimento.Tipo() is undefined
private abc(String string2, int n2) {
}
}
Related
Mycode is
public enum PartsOfSpeech2 {
n("noun"),
wp("标点"),
a("adjective"),
d("conjunction"),
...;
which I want
public enum PartsOfSpeech2 {
n("noun"),
wp("标点"),
a("adjective"),
d("conjunction"),
%("noun");
can I hava a default value which is not in it, can it be set as a default value?
because I have a type is "%", but enum is not support %, so I want a default value to solve it
The default for one who holds a reference to an enum without setting a value would be null (either automatically in case of a class field, or set by the user explicitly).
Unfortunately you cannot override the method valueOf for your own enum, as it is static.
But you can still create your methods:
public enum PartsOfSpeech2 {
n("noun"),
wp("标点"),
a("adjective"),
d("conjunction");
private String value;
PartsOfSpeech2(String value) {
this.value = value;
}
// declare your defaults with constant values
private final static PartsOfSpeech2 defaultValue = n;
private final static String defaultString = "%";
// `of` as a substitute for `valueOf` handling the default value
public static PartsOfSpeech2 of(String value) {
if(value.equals(defaultString)) return defaultValue;
return PartsOfSpeech2.valueOf(value);
}
// `defaultOr` for handling default value for null
public static PartsOfSpeech2 defaultOr(PartsOfSpeech2 value) {
return value != null ? value : defaultValue;
}
#Override
public String toString() { return value; }
}
From JLS 8.9. Enums
An enum type has no instances other than those defined by its enum constants. It is a compile-time error to attempt to explicitly instantiate an enum type (§15.9.1).
So you can't have any instance which is take default value.
You can create default constant and use that using some condition.
public enum PartsOfSpeech2 {
....
DEFAULT("DEFAULT");
}
And use condition to check if your string have constant, Ex "%" have enum or not. if not use default value:
PartsOfSpeech2 result = PartsOfSpeech2.valueOf("%"); //Your String EX: %
PartsOfSpeech2 resultNew = result==null?PartsOfSpeech2.DEFAULT: result;
The way I solved it was the following
public enum YourEnum{
ENUM1("stringToMatchWith"),
ENUM2("stringToMatchWith2"),
DEFAULTENUM("Default");
public final String label;
YourEnum(String label) {
this.label = label;
}
public static YourEnum resolveYourEnum(String stringToMatch) {
return Arrays.stream(YourEnum.values()).filter(aEnum -> aEnum.label.equals(stringToMatch)).findFirst().orElse(YourEnum.DEFAULTENUM);
}
That way you can do YourEnum.resolveYourEnum("aString") and return the specified enum or the default we set
In Java, getting this error:
Error: The constructor MyComplex(MyComplex) is undefined
Java Code:
public class MyComplex {
int realPart, imaginaryPart;
public MyComplex(){
}
public MyComplex(int realPart, int imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public void setRealPart(int realPart) {
this.realPart = realPart;
}
public String toString() {
return realPart + " + " + imaginaryPart +"i";
}
}
public class MyComplexTester {
public static void main(String[] args) {
MyComplex a = new MyComplex(20, 50);
MyComplex b = new MyComplex(a); //Error happens here
b.setRealPart(4);
System.out.println(b);
}
}
The code works fine if I use
MyComplex b = a;
But I can't change the code in the main method since its a homework on designing the class to run the given method.
Explanation
You do not have a constructor that accepts another MyComplex (copy constructor). You only created constructors that accept:
No argument, new MyComplex()
Two int arguments, new MyComplex(5, 2)
Solution
You need to explicitly define constructors that you want to use. Java does not generate such a constructor for you. For example:
public MyComplex(MyComplex other) {
realPart = other.realPart;
imaginaryPart = other.imaginaryPart;
}
Then it will also work.
Notes
In order to increase readability of your code, you should use explicit constructor forwarding for the new copy constructor and especially for your default constructor.
As an example, right now your default constructor new MyComplex() will lead to a complex value of 0 + 0i. But this can easily be overseen since your code does not clearly indicate that.
With forwarding, the intention is much clearer:
public MyComplex() {
this(0, 0);
}
public MyComplex(MyComplex other) {
this(other.realPart, other.imaginaryPart);
}
Then both will just forward to the explicit constructor that accepts the two int values.
Note that the only constructor Java auto-generates for you is the trivial default constructor. That is public MyComplex() { } (no arguments - does nothing). And only if you did not write any constructor yourself.
You should create the corresponding (copy) constructor.
So:
public MyComplex(MyComplex a){
realPart = a.realPart;
imaginaryPart = a.imaginaryPart;
}
you must have an overloaded constructor that accepts an object of type MyComplex to get this working.
below is your updated class
public class MyComplex {
int realPart, imaginaryPart;
public MyComplex(){
}
public MyComplex(int realPart, int imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public MyComplex(MyComplex mycomplex) {//this is the constructor you need
this.realPart = mycomplex.realPart;
this.imaginaryPart = mycomplex.imaginaryPart;
}
public void setRealPart(int realPart) {
this.realPart = realPart;
}
public String toString() {
return realPart + " + " + imaginaryPart +"i";
}
}
Because there is no constructor declared with MyComplex as argument. You need to declare the below constructor :-
public MyComplex(MyComplex mycomplex) {
this.realPart = mycomplex.realPart;
this.imaginaryPart = mycomplex.imaginaryPart;
}
Because in below line
MyComplex b = new MyComplex(a);
you are passing a which is of type MyComplex, but in MyComplex class you defined constructor with one parameter whose type is int. please correct your passing parameter.
Why am i getting an error "Constructor is undefined" is it in my eclipse IDE?
is there something wrong with my code?
public enum EnumHSClass {
PALADIN ("Paladin"),ROUGE("ROUGE");
}
If you expect your enums to have parameters, you need to declare a constructor and fields for those parameters.
public enum EnumHSClass {
PALADIN ("Paladin"),ROUGE("ROUGE");
private final String name;
private EnumHSClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
You need to provide a constructor in your enum like:
public enum EnumHSClass {
PALADIN("Paladin"), ROUGE("ROUGE");
String value;
EnumHSClass(String value) {
this.value = value;
}
}
Note: The constructor for an enum type must be package-private or
private access. It automatically creates the constants that are
defined at the beginning of the enum body. You cannot invoke an enum
constructor yourself.
Ref : http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Enums have constructors too, but only with either private or default visibility:
public enum EnumHSClass {
PALADIN ("Paladin"),ROUGE("ROUGE");
private EnumHSClass(String s) {
// do something with s
}
}
You may want to declare a field and create a getter for it, and set the field in the constructor.
Also note that the name of the enum instance is available for free via the (implicit) name() method that all enums have - maybe you can use that instead.
Your code should look like this:
public enum EnumHSClass {
PALADIN ("Paladin"), ROUGE("ROUGE");
private String name;
private EnumHSClass(String name) {
this.name = name;
}
}
public enum Days {
MONDAY(1), TUESDAY(2);
int val;
Days (int val) {
this.val = val;
}
}
Am trying to use Enum as Constants for readability as below .
Public enum x { test1 , test2 , test3 }
I want to pass this enum into a method and use it as a int value as shown
private void (int type)
{
switch(int)
{
case enum.X:
// do somthing
break;
}
} ;
Can we use enum here as its more clearer than using a int value .( like switch 1 etc) . Is it possible to use this way.?
Yes, you should be able to use an enum in a switch statement in Java:
public enum SomeEnum { FOO, BAR, BAZ };
// in a class...
private void something(SomeEnum s) {
switch (s) {
case FOO:
// do something
break;
case BAR:
// do something else
break;
}
}
Not sure I understand how int values tie into this, but you can have fields/methods on an enum like in a normal Java class, and can use these to hold int (or any other type) values as on any other POJO.
Here's an example in which we declare a constructor for an enum class, so that you can pass in values for internal variables at the time each instance of the enum is constructed. To help you follow what's going on: First we declare the items in the enum - each declaration invokes the constructor, so we can pass in instance variables here. Then the code for the enum class follows, as with a normal Java class.
public enum AnotherEnum {
ONE(1, "un"), TWO(2, "deux"), THREE(3, "trois"), FIFTY_SEVEN(57, "cinquante-sept");
private final int someInt;
private final String french;
private AnotherEnum(int i, String s) {
this.someInt = i;
this.french = s;
}
public int getSomeInt() {
return this.someInt;
}
public String getFrench() {
return this.french;
}
}
So for example, System.out.println(AnotherEnum.TWO.getSomeInt()) would print 2, and System.out.println(AnotherEnum.FIFTY_SEVEN.getFrench()) would print cinquante-sept.
No, you cannot say:
case Enumerator.ordinal():
But you could say:
switch(EnumObject.values()[intVar]) {
case Enumerator1:
...
}
Another way you can do this by doing a little more work with you enum class.
public enum Foo {
X (1),
Y (2);
private int value;
Foo (int value)
{
this.value = value;
}
}
Now all you need to do is:
switch (int)
{
case Foo.X: doSomething ();break;
case Foo.Y: doSomething ();break;
}
in an interface, I store constants in this way (I'd like to know what you think of this practice). This is just a dummy example.
interface HttpConstants {
/** 2XX: generally "OK" */
public static final int HTTP_OK = 200;
public static final int HTTP_CREATED = 201;
public static final int HTTP_ACCEPTED = 202;
public static final int HTTP_NOT_AUTHORITATIVE = 203;
public static final int HTTP_NO_CONTENT = 204;
public static final int HTTP_RESET = 205;
public static final int HTTP_PARTIAL = 206;
...
}
Is there a way I can iterate over all constants declared in this interface ?
Using reflection:
Field[] interfaceFields=HttpConstants.class.getFields();
for(Field f:interfaceFields) {
//do something
}
But anyway, if you can redesign your class, I would recomend you to handle a static enum constants construction. So, suposing your class will contain always an int value for every constant:
enum HttpConstants {
HTTP_OK(200), HTTP_CREATED(201), HTTP_ACCEPTED(202),
HTTP_NOT_AUTHORITATIVE(203),HTTP_NO_CONTENT(204),
HTTP_RESET(205), HTTP_PARTIAL(206) /* ... */;
private int value;
HttpConstants(int aValue) {
value=aValue;
}
public int getValue() {
return value;
}
}
Then, to loop on it:
for(HttpConstants val: HttpConstants.values()) {
int value=val.getValue();
//...
}
Thus, avoiding the access to the reflection API.
I would create these constants as an enumeration. Enums in Java can have their own fields and methods, which very convenient for your case. So I would do this the following way:
enum HttpConstant {
HTTP_OK(200),
HTTP_CREATED(201),
HTTP_ACCEPTED(202),
HTTP_NOT_AUTHORITATIVE(203),
HTTP_NO_CONTENT(204),
HTTP_RESET(205),
HTTP_PARTIAL(206);
private final int id;
HttpConstant(int id) {
this.id = id;
}
int getId() {
return id;
}
}
Now the iteration is easy:
for (HttpConstant constant : HttpConstant.values()) {
//Do something with the constant
}
This way it is also easy to add associate some new values with the constants, you just have to add new fields.
Right now you may use reflection:
Field[] interfaceFields = HttpConstants.class.getFields();
for (Field field : interfaceFields) {
int constant = field.getInt(null);
//Do something with the field
}
However, it is better to use the approach with enums because with reflection coding errors result in runtime exceptions instead of compile-time errors.
for(Field f : HttpConstants.class.getFields()){
int constant = f.getInt(null);
}
public enum HttpConstant {
/** 2XX: generally "OK" */
HTTP_OK(200).
HTTP_CREATED(201),
HTTP_ACCEPTED(202),
HTTP_NOT_AUTHORITATIVE(203),
HTTP_NO_CONTENT(204),
HTTP_RESET(205),
HTTP_PARTIAL(206);
private int code;
private HttpConstant(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
with HttpConstant.values().
Well usually when i have something like that i make a Map in the interface that has the keys - constant names with values constant - values.
And that's how i can iterate over them.
I'd like to know what you think of this practice
Consider using an enum instead of an interface with constants.
enum HttpResultCode {
HTTP_OK(200),
HTTP_CREATED(201),
HTTP_ACCEPTED(202),
HTTP_NOT_AUTHORITATIVE(203),
HTTP_NO_CONTENT(204),
HTTP_RESET(205),
HTTP_PARTIAL(206);
private final int code;
private HttpResultCode(int code) {
this.code = code;
}
public int getCode(int code) {
return code;
}
public static HttpResultCode forCode(int code) {
for (HttpResultCode e : HttpResultCode.values()) {
if (e.code == code) {
return e;
}
}
throw new IllegalArgumentException("Invalid code: " + code);
}
}