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!!

[2169 byte] By [Tjorriemorriea] at [2007-11-27 7:36:12]
# 1
You have to first create an object of MyEieTipe before you can add it to the the ArrayList. So your code will look like this:MyEieTipe obj = new MyEieTipe("hello");myTipe.add(obj);
DarumAa at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 2

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?

Tjorriemorriea at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 3

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") );

jhajasa at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 4
Yes, of course I forget name of new class ;)myTipe.add (new MyEieTipe("hello") );
jhajasa at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 5

> 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.

DarumAa at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 6

Thanks! That also doesn't seem to work....

ArrayList<MyEieTipe> myTipe = new ArrayList();

myTipe.add(new MyEieTipe("Hello"));//also still gives <identifier expected> error

oh boy......

PS: also tried it with taking out the void. also didn't work. gonna leave it as a constructor (without the void) from now on.

Tjorriemorriea at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 7

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;

}

}

DarumAa at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 8

Thanks! this is green for go!!

public MyEieTipe(String startNaam) {

dieNaam = startNaam;

dieTelling = 0;

dieVegte = 0;

dieHide = false;

dieBeskerm = false;

}

}

ArrayList<MyEieTipe> myTipe = new ArrayList();

MyEieTipe obj = new MyEieTipe("hello");

Tjorriemorriea at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...
# 9

I also noticed that when you are declaring Generics, they need to be declared on both sides of the equality for generics to work properly:

ArrayList<MyEieTipe> myTipe = new ArrayList(); //wrong

ArrayList<MyEieTipe> myTipe = new ArrayList<MyEieTipe>(); // right

DarumAa at 2007-7-12 19:16:42 > top of Java-index,Java Essentials,New To Java...