aspose file tools*
The moose likes Beginning Java and the fly likes Could we create an object in type List? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could we create an object in type List?" Watch "Could we create an object in type List?" New topic
Author

Could we create an object in type List?

Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
Hi,

If we are giving--
List<Smtable> svalues = new List<Smtable>();
it will show some Error.
But...
if we are creating an object in ArrayList like this--
ArrayList<Smtable> svalues = new ArrayList<Smtable>();
no Error is shown.

Why is it so? can't we create an object in type List?

And when it is given like this---
List<Smtable> smlabels = jobmgr.getSmvalues();
it wont show any Error and works fine.

Could anyone help me?


Thanks
-------
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

java.util.List is an interface. You cannot "new" or instantiate an interface. However, java.util.ArrayList is one implementation of a List interface, meaning...

public class ArrayList implements List { ... }

And when it is given like this---
List<Smtable> smlabels = jobmgr.getSmvalues();
it wont show any Error and works fine.


Because jobmgr.getSmvalues() probably returns some implementation of List, like an ArrayList.


GenRocket - A Test Data Generation Platform
Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
Thank you Gregg
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could we create an object in type List?
 
Similar Threads
Collection Query
Generic Question: wildcard with super
Generics doubt
Generic Method from K&B Master Exam
generics - incompatible types