Stange compilation error.

Hello,

I am going through the Generics pdf off the Java API and thought I would try some of the sample code that it contains. I created this class

import java.util.*;

public class Test

{

public static <T> void copy(List<T> dest, List<? extends T> src)

{

}

public Test()

{

}

}

class Test2

{

public static <T, S extends T> void copy(List<T> dest, List<S> src)

{

}

}

interface Part

{

}

class Guillotine implements Part

{

}

class Blade implements Part

{

}

class Main

{

Collection <Part> c = new ArrayList<Part>();

Part a = new Guillotine();

Part b = new Blade();

c.add (a);

c.add (b);

Collection <Part> k = c;

}

A error apon compilation id generated for the lines

c.add (a);

c.add (b);

Y:\>javac Test.java

Y:\>javac Test.java

Test.java:38: <identifier> expected

c.add (a);

^

Test.java:39: <identifier> expected

c.add (b);

^

If I change thos two lines to

boolean c1 = c.add (a);

boolean c2 = c.add (b);

It compiles without any errors. What the deal? Do you have to colelct the return data from a Collection.add? I've been scatching my head over this for a few hours and have no idea. I have never had this issue before. I have stripped all the generics code out and it still doesn't compile.

thanks,

Tyson Oswald

[1603 byte] By [tyson.oswalda] at [2007-11-27 2:56:23]
# 1

The class body can contain only declarations of methods and variables and things like that. All actual executable code has to be placed in a method, constructor, or an initializer block.

You probably want to implement a "main" method in your program http://java.sun.com/docs/books/tutorial/getStarted/application/index.html

jsalonena at 2007-7-12 3:34:01 > top of Java-index,Java Essentials,Java Programming...
# 2

> ...

> class Main

> {

> Collection <Part> c = new ArrayList<Part>();

> Part a = new Guillotine();

> Part b = new Blade();

> c.add (a);

> c.add (b);

> Collection <Part> k = c;

> }

>

> ...

I cannot imagine that the above code compiles.

prometheuzza at 2007-7-12 3:34:01 > top of Java-index,Java Essentials,Java Programming...
# 3
Doh! Well I guess that's what I get for looking at for so long. I didn't notice it wasn't defined within a method. I guess it's a good lesson so it's not all bad.thanks for pointing it out!Tyson
tyson.oswalda at 2007-7-12 3:34:01 > top of Java-index,Java Essentials,Java Programming...