Hello I am very new to java and I want to use a constructor, pass it with a parameter from another class.
But eclipse is giving me an error "The type of the expression must be an array type but it resolved to String".
String sType = type[0];
String mName = type[1];
for the above
please could someone help me with the below and point out my mistakes?
public class M { String [] type= new String [2]; public M (String type) { this.Type = Type.split("\\."); String sType = type[0]; String mName = type[1]; System.out.println(sType); } }
Well, there are two problems here. First, you've got:
this.Type = Type.split("\\.");
... which uses Type
instead of type
.
Next, you're using type[0]
which is referring to the parameter called type
, not the field.
You could fix it to:
public M(String type) {
this.type = type.split("\\.");
String sType = this.type[0];
String mName = this.type[1];
System.out.println(sType);
}
... but I'd seriously consider changing the name of the fields to types
instead of type
- or possibly a custom class, if this is an array that is always expected to have two elements with a specific meaning.
(I'm also hoping that your class isn't really called M
...)
See more on this question at Stackoverflow