identifier expected error with custom class component in arraylist
Hi, this is my 7th day of learning java, so please bear with me if I use the wrong names, etc
I need to make my own type for what I want to do.
Then I want to make an ArrayList of it (because I don't know the size).
I need my new type to contain 5 variables (or is it fields?, wait, or is it elements? *sigh*) and have the user only give the first value for each element in the array, which is a string. the other fields must be automatically (initialized?) to the values I want.
1- am i declaring those default values at the right place?
1a - is the void in the constructer correct programming?
2- this is what is my problem. why do i get the <identifier expected> error? what's wrong with my argument? it is a string, so i'm stumped here.
publicclass MyEieTipe{
//the MyEieTipe class has 5 variables currently
public String dieNaam;
publicfloat dieTelling;
publicint dieVegte;
publicboolean dieHide;
publicboolean dieBeskerm;
//the MyEieTipe class has 1 constructor
publicvoid MyEieTipe(String startNaam){
dieNaam = startNaam;
dieTelling = 0;
dieVegte = 0;
dieHide =false;
dieBeskerm =false;
}
}
ArrayList<MyEieTipe> myTipe =new ArrayList();
myTipe.add("hello");//gives <identifier expected> error
Any help greatly appreciated!!
Thanks! It still gives the error though...
ArrayList<MyEieTipe> myTipe = new ArrayList();
MyEieTipe obj = new MyEieTipe("hello");
myTipe.add(obj);//still gives <identifier expected> error
PS: i'm first language isn't English, so that class name is actually very descriptive in my language, unless you meant something else?
ad 1) Yes - constructor is the right place to initialize properties (data variables in a object).
ad 1a) Sorry - you have to declare it without type! If you include any type (like void in this case) you didn't declare constructor but normal method (by coincidence using same name as class). You must declare constructor like this:
public MyEieTipe(String startNaam)
ad 2) I think you want to add new object of the class MyEieTipe, right? So you have to make it:
myTipe.add (new("hello") );
> Thanks! It still gives the error though...
>
> ArrayList<MyEieTipe> myTipe = new
> ArrayList();
>MyEieTipe obj = new MyEieTipe("hello");
> myTipe.add(obj);//still gives <identifier
> expected> error
>
>
> PS: i'm first language isn't English, so that
> class name is actually very descriptive in my
> language, unless you meant something else?
Delete the void identifier from your constructor and the code should work. By adding void you are declaring a normal function which has the same name as your constructor. When you call new to create a new object of your class, the constructor that you are trying to call is not invoked because the java compiler considers it a normal method.
Hope this helps.
Here is the working code:
import java.util.*;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<MyEieTipe> myTipe = new ArrayList();
MyEieTipe obj = new MyEieTipe("hello");
myTipe.add(obj);
}
}
class MyEieTipe {
//the MyEieTipe class has 5 variables currently
public String dieNaam;
public float dieTelling;
public int dieVegte;
public boolean dieHide;
public boolean dieBeskerm;
//the MyEieTipe class has 1 constructor
public MyEieTipe(String startNaam) {
dieNaam = startNaam;
dieTelling = 0;
dieVegte = 0;
dieHide = false;
dieBeskerm = false;
}
}