Forums Register Login

Can I instantiate an Interface?

+Pie Number of slices to send: Send
The answer is No. But I can do this

List<int> l = new List<int>();

Why?
+Pie Number of slices to send: Send
When you do new List<Integer>(){...}; you create a anonymous class. [=

http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm
http://mindprod.com/jgloss/anonymousclasses.html
1
+Pie Number of slices to send: Send
Can you really compile that code?
+Pie Number of slices to send: Send
Creating an anonymous inner class is nothing but implementing that particular interface and you would have to implement all the methods present in the interface.

The given code is no where close to that. Moreover the type used in the Generic declaration has to be a class/reference type.
+Pie Number of slices to send: Send
 

Mohamed Sanaulla wrote:Can you really compile that code?


It won't. Even if there were a non-abstract List class with a generic type (java.util.List is an interface, java.awt.List is not generic), the <int> is still not allowed because primitive types can never be generic types.
+Pie Number of slices to send: Send
Sorry, the code does not compile. My question was can we use primitive in generics.
+Pie Number of slices to send: Send
 

Jane Somerfield wrote:Sorry, the code does not compile. My question was can we use primitive in generics.


I think Rob just mentioned about this in his post and also I have mentioned it in my second post.

I was struggling to remember the "primitive" keyword though
+Pie Number of slices to send: Send
 

Jane Somerfield wrote:My question was can we use primitive in generics.


No.

Winston
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 969 times.
Similar Threads
having issue getting the same level nodes.
K&B generics doubt
confuse statement ?
pre-select in option
Three Dimensional ArrayList?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:34:11.