Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Using optional generics

Thread view: 
Mize-ze - 01 Mar 2007 10:23 GMT
Hello,

I am intrested in creating a generic class which leaves the use of
generics optional.
I want to use the generics when I want and leave it out when I want,
just like java.util.Map
How can I?

Example:
------------------------------------------------------------------------------------------------------

public class foo<T>{
  private T value;
}

public static void main (String argv[]){

foo<Integer> = new foo<Integer>();  // That's ok
foo = new foo();                             // Can't do this now!
how can I?
}
-----------------------------------------------------------------------------------------------------

Thanks
Hendrik Maryns - 01 Mar 2007 11:23 GMT
Mize-ze schreef:
> Hello,
>
[quoted text clipped - 10 lines]
>    private T value;
> }

Please follow the naming conventions, especially if you post here:
public class Foo<T>

> public static void main (String argv[]){
>
>  foo<Integer> = new foo<Integer>();  // That's ok

Are you sure?

>  foo = new foo();                             // Can't do this now!

Of course not, for the same reason as the line above.

> how can I?

Don’t forget to give your variables names.

> }
> -----------------------------------------------------------------------------------------------------
>
> Thanks

You’re welcome, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Ian Wilson - 01 Mar 2007 13:22 GMT
> Hello,
>
[quoted text clipped - 18 lines]
> }
> -----------------------------------------------------------------------------------------------------

Your example doesn't compile so I surmise it is retyped and contains
numerous typos. I've therefore no way to know what is wrong with your
real code.

public class Foo<T> {
    private T var;
    public static void main(String[] args) {
        Foo<Integer> foo1 = new Foo<Integer>();
        Foo foo2 = new Foo();
    }
}

Compiles and runs here.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.