Results 1 to 6 of 6

Thread: Getting warning in java generics

  1. #1
    Join Date
    Mar 2010
    Posts
    202

    Getting warning in java generics

    Hi,
    I wonder why in this particular case, I am getting a warning in the java generics
    Code:
    public class GenericsTest <T> (
    
    public void call () (
    		method (<T> new ArrayList (), new LinkedList <T> ());
    	)
    
    public void method (Collection <T> ... c) ()
    
    )
    If you have any solution for this then please post back and help me. Thanks for your help.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Getting warning in java generics

    Hello,
    I walk maybe a little, but given that the mechanism is based precisely on an array, it can not combine template and table. we do not see the picture, but what is done at compile time. The error message may indicate that you can not mix and templates, but apparently your question why it is forbidden, rather than what it is prohibited? Hope you are getting the point what i am trying to explain here.

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Getting warning in java generics

    Hello,
    Just to complete the response deltree in principle it is not possible to declare an array of parameterized type.
    A statement something like this
    Code:
    MyClass <String>[] a = new MyClass <String>(10);
    will generate an error. This restriction is related to the internal mechanism in the JVM, called erasure for generics. Hope this will help you and i guess you know the basics of the java very well that is the core java.

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Getting warning in java generics

    Hello,
    I completely agree with the above post and would like to add some more points here. At runtime, there is no generic type in the JVM, which is a type <String> MyClass [] becomes a simple MyClass [] while running after erasure. We can convert the Object []
    Code:
    Object[] table = a;
    But as we know that array remembers its error component and launches ArrayStoreException when trying to store an element of wrong type. Allow array type generics pass compilation, but could create runtime errors, which would remove the safety provided by generics.

  5. #5
    Join Date
    Mar 2010
    Posts
    202

    Re: Getting warning in java generics

    Hello,
    Here is my updated code , have a look at it and if you find any errors in this then please guide me with the correct.
    Code:
    Public class GenTest <T> {
     
    	Public void call() {
    		/ / It does not generate the type new List (because it is not possible)
    		/ / This call compiles.
    		method(new List[] { new ArrayList <String>(), new LinkedList <Integer>() });
     
    		/ / It tries to put the same thing directly in varargs, it does
    		/ / Compile
    		/ / Because the compiler performs type checking safe
    		/ / It is therefore no way to get the wrong type
    		/ / Execution, hence the futility of warning
    		method(new ArrayList <String>(), new LinkedList <Integer>());
    	}
     
    	Public void method(Collection <T> ... p) {
     
    	}
     
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Getting warning in java generics

    Hello,
    Not because the problem is not the call of the method, but how it will use in the method. The method is not safe and the warning is logical in some way (using the logic of Generics that prevent any probable problem). The compiler could check the code for the method to check if it is safe or not, but it's not really the role of a compiler but a code checker. Hope you understand the ting well here.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Generics vs. Reflection in Java
    By Jagruti23 in forum Software Development
    Replies: 9
    Last Post: 15-09-2010, 09:36 PM
  3. Generics problem in java
    By Gillian Anderson in forum Software Development
    Replies: 4
    Last Post: 31-03-2010, 01:08 PM
  4. System warning code in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 11:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,561,530.97626 seconds with 16 queries