Generics and creating new objects

Assume this class:
class Yatta<T>
  public Yatta()
    T t = new T();
}When compiling I get "unexpected type" refering to the "T" in "new T()". Why isn't this allowed? Is there any way of doing something similar without creating a huge chain of factories for each type?

Please use single letters for type parameters. Doing what you've done with "Type" makes reading the code difficult...
Since duplicate() is declared in Component I assume it is declared to return Component. How can the compiler possibly know that it is going to return a more specific type? It can't. (Well... you can tell it... see below)
Because of what I wrote in the first paragraph, it's not immediately obvious that Type is a type parameter, but it is, and casting to a type parameter doesn't get you anywhere. From this code and your original post, you appear to be treating Java Generics like C++ templates. They are not the same. In particular, Generics are implemented using a technique called erasure which means that the type parameter information is not present in the compiled class files. The practical upshot of this is that the cast in the line you highlight is in fact a cast to Component (since that is the upper bound of Type) and this cast is useless because we already know the c is a Component. So the compiler is correctly warning you that the typecast will not actually perform the check that you want it to.
(In fact, if you try this in Eclipse, you get a better warning: "Type safety: The cast from Component to T is actually checking against the erased type Component")
So... how to fix this...?
Well, you need to make the return type of duplicate() change depending on the concrete implementation. You could do this by parameterising Component thus:
  public interface Component<T extends Component<T>> {
    public T duplicate();
  }Now, every use of Component will need to be parameterised. In the case of the code you have below, you'll need:
public class Container<T extends Component<T>> extends ArrayList<T> {
  public Container() {
    //Empty
    // Why write it then??? Get rid of it.
  public Container(Container<T> object) {
    Iterator<T> i = object.iterator();  // don't need ListIterator here
    while(i.hasNext()) {
      add(i.next().duplicate());
}and every implementation of Component needs to look like this:
public static class Something implements Component<Something> {
  public Something duplicate() {  // note the return type
    return null;  // or something more useful!
}Hope that helps.
(PS 'tabs' are the work of satan and should never be found in source code)

Similar Messages

  • What is the diffrence between extends and creating new object?

    HI ALL,
    what is the diffrence between extends and creating new object?
    meaning
    class base{
    class derived extends base{
    class base{
    class derived {
    derived(){
    base var = new base();
    can u people tell me diffence from the above examples.
    THANKS.
    ANANDA

    When you create a new object you have to supply the class to which that
    object belongs. A class can extend from another class. If it does so
    explicitly you can define the 'parent' class from which the class extends.
    If you don't explicitly mention anything, the class will implicitly extend
    from the absolute base class named 'Object'.
    Your example is a bit convoluted: when you create a Derived object,
    its constructor creates another object, i.e. an object from the class from
    which the Derived class extends.
    Extending from a class and creating an object don't have much in common.
    kind regards,
    Jos

  • Problem in creating new objects in ABAP workbench

    Hi ,
        I am getting error in a newly installed SAP system when try to create new objects in ABAP workbench. The error is like'No changes for the user allowed'.
    Can you tell me what the error is here? What can be done for this so that I can create new object.
    Thanks,
    Manas

    Hi,
    Run tcode SU01. (Provided if u hav authorization to run this Tcode)
    Enter SAP* as user and click on the Copy button.
    Enter the name for your new user in the dialog box and click Continue
    Enter a password for your new user.
    Goto Profile sheet and adjust the profiles to be SAP_ALL and SAP_NEW.
    Save the user and logon with it.
    Now you'll be able to do your changes.
    Reward Points if useful.
    Regards
    Rose
    Edited by: Rose Preethi on Mar 31, 2008 5:20 PM

  • How to create new object key in BDS?

    Hello,
    I need to create custom class in BDS (OT type) and object key for it.
    In this forum (and in the whole Internet) I found information only about custom class. However there's nothing about object key.
    How could I create new object key in BDS class?
    Great thanx in advance!

    Hi Max,
    you can create BOR object in business object repository, Transaction SWO1, SWO2, search documentaion for BOR obkect.
    Regards,
    Clemens

  • How can I make the popup with empty fileds and create new record?

    I would like to use a popup to create new record.
    I created a af:popup by drag and drop a VO from data control to jsff. then, I created a button and place a af:showPopupBehavior. I was able to popup window by click the button.
    however, the window filled with the information from the 1st record. and when I select a record in table and click popup, the popup is filled with that record.
    How can I make the popup with empty fileds and create new record by saving the popup?
    Thanks

    You can have edit and new buttons, in the PopupFetchEvent identify button source (using popupFetchEvent.getLaunchSourceClientId()) if new button clicked clear the binding using below code.
    If you want to see empty fields, in the popup PopupFetchEvent clear the input component bindings.
    resetBindingValue("#{bindings.<componentid>.inputValue}", null);
        public static void resetBindingValue(String expression, Object newValue) {
            FacesContext ctx = FacesContext.getCurrentInstance();
            Application app = ctx.getApplication();
            ExpressionFactory elFactory = app.getExpressionFactory();
            ELContext elContext = ctx.getELContext();
            ValueExpression valueExp = elFactory.createValueExpression(elContext,expression,Object.class);
            Class bindClass = valueExp.getType(elContext);
            valueExp.setValue(elContext,newValue);
        }

  • Resuse existing object (Sp,tables etc)or create new object if that is possible?

    Hello Everyone,
    Is it a good practice to create new objects (stored procedures, tables etc.) or modify and reuse the existing object if that can be done?
    Regards
    Regards

    If you need simple add a new column to the SELECT statement so there is no need to create a new stored procedure/view for this
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • I have a new email address. Changed this on my itunes account. On iPhone deleted old icloud account and created new with new email. I have set find my phone to on. When I download music it no longer goes to icloud an does not show up on my iphone.

    I have a new email address. Changed this on my itunes account. On iPhone deleted old icloud account and created new with new email. I have set find my phone to on. When I download music from itunes it no longer goes to icloud an does not show up on my iphone.

    I have the same problem - it is maddening. I rely on this iPad for work so this is not just an annoyance! The above solutions of changing the appleid on the device or on the website do not work.
    The old email address no longer exists - I haven't used it in a year probably and I no longer have the account.  I logged into the appleid website and there is no trace of the old email address so there is nothing that can be deleted or changed there.  On the iPad there is no trace of the old email address so nothing can be deleted there either. I have updated the iPad software and the same problem comes right back.  Every 2 seconds I am asked to log in using the old non-existent email.  The device is currently useless.
    The only recent change to anything was the addition of an Apple TV device, which was set up using the correct login and password.
    Does anyone have any ideas? The iPad has been backed up to the iCloud so presumably it now won't recognize the current iCloud account? So restoring may notbe an option?

  • Copying home DVD videos, editing, and creating new DVDs

    Here is the situation:
    I have the 17" and this is my first Mac to own. I am computer literate with most my experience on Windows and Linux.
    That being said, I am trying to edit home videos; but they are already on DVDs. I got a LiteOn DVD/VCR combo recorder for my entertainment center. I have a camcorder that uses Hi-8 media and it is only able to use S-video/composite outputs (no USB or Firewire). I am using the LiteOn recorder to archive all my videos to DVDs because it has composite inputs. The LiteON has hardly any editing capability. So I want to take the "simple" DVDs I made, copy them to my HD, edit them (to cut out junk, create better menus, and give it as nice touch), and create new DVDs to distribute to my family and to get rid of old tapes.
    So in short, the process I want to do on the Mac is:
    1. Copy my family home DVDs
    2. Edit the video.
    3. Create more "creative" DVDs for my family.
    I have been looking around the website and reading posts and I don't see how to do this. I did try to create an image on disk utility, but it failed for whatever reason (maybe I am missing a step). How can I accomplish the three steps above?
    Please help.
    Thank you, Michael

    If these are true, standard video DVDs that the drive creates, there are a number of utilities that can extract and convert the videos for use in editing applications. A couple of utilities that people have reported success with are DVDxDV, Cinematize, and MPEG Streamclip (MPEG Streamclip is freeware but requires that you have Apple's QuickTime MPEG2 Playback Component), and there are no doubt others.
    Once extracted, you can use iMovie to edit the clips and iDVD to burn them to a new DVD. Then you can use Disk Image or a third-party application such as Roxio's Popcorn to make additional copies.
    Hope this helps.

  • Transfer assets collectively to new class and create new assets like ABUMN

    We need to change the account determination for a few Asset Classes that already have assets with postings.  As a result we must create new asset classes and assign a new account determination.
    Is there a program that would allow transfer assets collectively to a new asset class and create new assets automatically similar to ABUMN?  T-code ABUMN allows you to transfer multiple assets but you must create the new assets first.  ABUMN will transfer and create new asset but only does one asset at a time. 
    There are hundreds of assets with postings to be transferred from the old asset classes to the corresponding new asset classes.
    Appreciate whatever input you can provide.
    Tara

    download the asset master data(original) and create new asstes using lsmw or any third party tool also.

  • Reconfigure sales BOM deletes line item and creates new line item

    We are having issue with Sales BOM. Once sales BOM is reconfigure, line item is automatically deleted and creates new line item. These changes are not captured by reporting tool. Is it standard SAP behavior? if its not standard behavior, how to fix this issue?

    Hi,
    If i am not wrong, i assume you are using site instead of plant.   If it is plant , then "yes", we can have a order with same material being line item 10 and 20 with different plants.  Reg delivery, if the plants are configured to a same shipping point then you will have one delievery.  if the plants are configured for a different shipping point , then delivery will be split up.
    Reward if it helps,
    Thanks & regards,
    CLN

  • Problem when creating new Objects in ESR

    Hi ,
    When i am trying to create new objects in ESR it is no allowing me to do,i have developed around 80 interfaces as of nowmsuddenly this eeror i getting,anyone experienced this kind of issue.
    *You cannot create new objects since the system is not the original system of the software component version*
    You cannot create new objects since the system is not
    the original system of the software component version (NOT_ORIGINAL_NO_CREATE)
    MESSAGE ID: com.sap.aii.ib.server.oa.rb_all.NOT_ORIGINAL_NO_CREATE
    Regards,
    Raj

    Hi Raja,
    I am new to SAP PI. So i dunno much regarding SAP PI. Hence i am getting same error that not able to create objects in SWCV. I read this discussion but i dunno where to select the 'Modifiable & Original' options, so that i can enable the Objects. Can you please guide me?
    Thanks,
    Amarnath

  • Dynamic Action-Delimiting a record and creating new record

    Hi ,
    My requirement is in dynamic Action I need to delimit all records in a Subtype of an Infotype and create New record for these records with BEGDA as ENDDA + 1 of the delimited records.
    Iam able to delimit all records in the Subtype but , Iam not able to create new records.
    My code for delimiting is as given below, in T588Z,
    9901     2          06     801     P     P9901-SUBTY='2'
    9901     2          06     802     P     PSYST-IOPER='MOD'
    9901     2          06     803     P     P9901-STATS='X'
    9901     2          06     804     P     P9901-PREAS='91'
    9901     2          06     805     F     DELIMIT_DTE(ZHSRAS_T588Z_9901)
    9901     2          06     806     W     P9901-ENDDA=RP50D-ZZDATE3
    9901     2          06     807     I     MOD,9901,3
    9901     3          06     808     P     SY-UCOMM='UPD'
    9901     3          06     809     F     DELIMIT_DTE(ZHSRAS_T588Z_9901)
    9901     3          06     810     W     P9901-ENDDA=RP50D-ZZDATE3
    The above code is working fine.
    My code for creating new record is as given below, even though the control is oing to the subroutine, INS statement is not working, and hence new record is not getting created.
    9901     3          06     811     P     SY-UCOMM='UPD'
    9901     3          06     812     P     PSAVE-ENDDA<>P9901-ENDDA
    9901     3          06     813     P     P9901-STATS<>'X'
    9901     3          06     814     I     INS,9901,3
    9901     3          06     815     F     BEGIN_DATE(ZHSRAS_T588Z_9901)
    9901     3          06     816     W     P9901-BEGDA=RP50D-ZZDATE3
    9901     3          06     817     W     P9901-ENDDA='99991231'
    Kindly help me on this.

    Hi shahana,
    1. I have not worked on this dynamic action.
    2. But instead of doing in 2 steps,
       it is usually done in 1 step only, manually.
    3. If we CREATE record from some date,
       then the OLD will AUTOMATICALLY get Delimited.
    (We don't have to do anything specially for delimiting
      the old record)
    regards,
    amit m.

  • Forgot security questions, how do i delete old ones and create new ones

    forgot security questions on, how do i delete old ones and create new ones

    The following has instructions: If you forgot the answers to your Apple ID security questions

  • TS2446 My child wants her own Apple ID for her new phone and mac book. Not sure of steps to disable and creat new account while transferring photos etc. thoughts ?

    My child wants her own Apple ID for her phone and mac book. Not sure of steps to disable and create new account while transferring photos etc. thoughts ?

    Welcome to the Apple Community.
    Unfortunately, if Apple simply allowed people to log into different accounts and download content, everyone would be using everyone could share everyone else's content and bypass the need to purchase anything. To protect against this, they limit you to logging into a new account to once every 90 days.
    If two or more of you, have different accounts and use only one computer, you should each have your own user account on the computer, to avoid the need to log in and out of iTunes.

  • Problem in Shrink my local disk C and create new partitions on it.

    I am trying to Shrink my local disk C (current boot volume) and create new partitions on it. But I am getting a Warning message that If I do so, I won't be able to boot from any volume excepet the the current boot Volume. 
    In case if I need to do the recovery for my computer what will happen. does it mean that I can't do the recovery from my hard disk?
    This question was solved.
    View Solution.

    Kaj_R wrote:
    I am trying to Shrink my local disk C (current boot volume) and create new partitions on it. But I am getting a Warning message that If I do so, I won't be able to boot from any volume excepet the the current boot Volume. 
    In case if I need to do the recovery for my computer what will happen. does it mean that I can't do the recovery from my hard disk?
    Hello Kaj_R.
    If you change the volumes it is quite likely that the recovery partition will not function.  I suggest you create a set of Recovery Media before messing with the partitions.
    This document can help you create the recovery media.
    Have a great day!
    Please click the white star under my name to give me Kudos as a way to say "Thanks!"
    Click the "Accept as Solution" button if I resolve your issue.

Maybe you are looking for

  • PI 2.0 Manager Report for Alarm Assignments and Acknowledgements

    Is there a Prime Infrastructure 2.0 manager report that shows daily alarms, who the alarms were assigned to and when they were acknowledged? I can't find anything on the tool or in the documentation and this is required to prove compliance in the fin

  • Download from internet with lingo

    Can I write lingo that will allow a Director movie to download a file from the internet and put it, say, onto the desktop of the user's computer?  I don't mean in a "stealth" fashion.  I mean a Director movie that the person chooses to run because th

  • Hooking up external dvd player

    I am trying to use an external dvd player ( memorex ) I have tried playing two dvd windows based games ( quake 2 and rage from 1998 ) my laptop only shows it as a music cd. IS there settings i can change to be able to play games?

  • Hi guys there is  a problem in my phone

    Hi in my iphone 5 the apps are nit downloading from app store and the updates are also not updating

  • Call an other dynpro after list-process. with a button in the functionmenu

    Hi expert-forum-members in my report I start a DynPro after the starting-point "start-of-selection": START-OF-SELECTION. CALL SCREEN 100. In the screen 100, I want to show a simple list like that: module pbo_0100 output.   SET PF-STATUS 'SCREEN_100'.