• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is the code giving compiler error

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!!
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ramchandra,

thanks a lot for the link.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need to cast because, "E" could be any type, not necessarily of type animal.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?


 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic