• Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to understand a Generics Wild Card concept  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I saw one program using Generics Wild Card, I am not able to understand it. Can anyone please explain what is going on with this:



it is giving this error:

WIldGenerics.java:11: cannot find symbol
symbol : class Dog
location: class Bar
list.add(new Dog());

isn't it supposed to take all the data types? "?" means all types, right?

thanks

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it is telling you that it doesn't know what the class Dog is - this has nothing to do with your list or generics.

try creating a Dog object on line 4, before you declare your List, and you will (i believe) get the same error.

Do you have a Dog class defined somewhere?
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I believe it is telling you that it doesn't know what the class Dog is - this has nothing to do with your list or generics.

try creating a Dog object on line 4, before you declare your List, and you will (i believe) get the same error.

Do you have a Dog class defined somewhere?


No, Dog class was not there. I created a plain Dog class;




but the error is still the same.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you create the Dog class? Is it in its own file? Do you have a CLASSPATH variable set?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still your information is not sufficient. how are you running your java? for instance, all the classes in same package?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete all old .bak and .class files then recompile this source file,hope you'll get new compile error next time.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Delete all old .bak and .class files then recompile this source file,hope you'll get new compile error next time.

but if javac find Dog class then it compiles... javac works on time stamp if new .class or .java find for Dog then it pick latest one...
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
saloni jhanwar wrote:Delete all old .bak and .class files then recompile this source file,hope you'll get new compile error next time.

but if javac find Dog class then it compiles... javac works on time stamp if new .class or .java find for Dog then it pick latest one...


May be,but in case of IDE doesn't work with time stamp, so it happens rarely but sometimes.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:
May be,but in case of IDE doesn't work with time stamp.

I am not sure. IDE dont create .bak files. i know editplus creates such one...
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K, first of all, am not using ide, am using notepad. Secondly, the classes are in same file, class path is given for current directory, as the main class is able to run.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and error as in below?
WIldGenerics.java:11: cannot find symbol
symbol : class Dog
location: class Bar
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry , my bad. , I meant to say that the class is found by the compiler, and hence it is showing the errors. one more thing.. I know that this code will fail at runtime, what I don't know is that why it is failing at compile time. as wild card should be taking just any datatype..
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As "?" stands for Any type. It will accept any element of List as a actual argument or also during variable declaration, but when we are trying to change the object than it don't allow.
Because, we do not know what is exactly "?" .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!