Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is the code giving compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is the code giving compiler error" Watch "Why is the code giving compiler error" New topic
Author

Why is the code giving compiler error

Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129


Can someone please explain why Line 1 gives a comliler error.
source: http://java.sun.com/docs/books/tutorial/java/generics/QandE/generics-answers.html

Explanation give is:
The compiler doesn't know the type of animal stored in house so the setAnimal method cannot be used, which I didnot understand, can someone please explain.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

first of all you must be know that..compiler always check the reference Type of house..since at the creation time you have not declared it..!!!
so till this time compiler doesn't know the value of E..!!
at line 1: you are assigning : E x = new Cat(); ?? Here compiler doesn't know the value of E ?? it doesn't know about the animal...so complaining you the same..!!

since we know that (new Cat() ) --> belongs to Cat..!! so why not compiler..!! we can make the compiler to deduce the class information..!!
but for this we have to modify the method declaration something like this:

public void < E > setAnimal( E x ) { }

I hope this will work now..!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hi all,

Sunny did you try to compile your code? It doesn't work.
The declaration public void < E > setAnimal( E x ) { }
is not valid because the generic type should be placed before return type. The declaration public < E > void setAnimal( E x ) { } would be valid. But there is an another problem: the type E within the method can differ from the E type declared within the class so you cannot assign animal = x. Your suggestion is wrong.

Now back to the original question. I think the problem is the wildcard <?> in the gereric type of reference variable house. To use use the method setAnimal() generic type <E> can't have a wildcard ? or ? extends Foo or ? super Foo. You can just use house with exact type <Cat> or without type at all. If you also change the line
AnimalHouseGeneric<?> house = new AnimalHouseGeneric<Cat>();
to
AnimalHouseGeneric<Cat> house = new AnimalHouseGeneric<Cat>();
or
AnimalHouseGeneric house = new AnimalHouseGeneric<Cat>();

the code compiles without problem.


SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Thanks Serg, so the moral is anytime we use a method like:
which do not accept a wild card as its parameters, we cannot pass something like

to the methods as parameters.
[ January 31, 2008: Message edited by: Dean Jones ]
ramchandra sugasi
Greenhorn

Joined: Jan 16, 2008
Posts: 10
you could get the best insight into java generics from the following link:

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

The best things are that it's just 23 pages and is written by
Gilad Bracha, person from Sun Microsystems.

Try to write the programs in examples in the pdf and you will learn better.
As you read on, just dont spoil your minds as you know that Java
can still be understood without complete knowledge of generics.
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hi ramchandra,

thanks a lot for the link.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchesrs,

i have modify the code little bit

public class AnimalHouseGeneric<E>
{
private E animal; //dec 1
public void setAnimal(Animal dog) // method 1
{
animal = (E) dog;
}
public E getAnimal() // method 2
{
return animal;
}

public static void main(String args[])
{
AnimalHouseGeneric<Cat> house = new AnimalHouseGeneric<Cat>();
//AnimalHouseGeneric house = new AnimalHouseGeneric<Cat>();

house.setAnimal(new Dog()); // line1
Cat c = house.getAnimal();
System.out.println(c);
}
}
class Animal
{

}
class Cat extends Animal
{
}
class Dog extends Animal
{
}

Can you tell me what has been declare in dec 1 and i have modify method 1 why we need it to cast to E ?

Can you please expalin me little in detail?

Thanks in advance..



Thanks<br />Dinesh
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
We need to cast because, "E" could be any type, not necessarily of type animal.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks for reply,
But i have read in k&b that "E" stands from element and "T" stands fro Type.
Is this question related to this ?

Please shed some lights on this ?


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is the code giving compiler error