PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to resolve: parameter never used with T type while T itself is used?

 April 28, 2022     generics, java, parameters, types, warnings     No comments   

Issue

I am facing with a Java Warning ("Parameter myParameter is never used") on my generic method where I use type of the parameter (T) but not the parameter value itself. Can I avoid this warning without using SuppressWarnings annotation?

private <T extends MyInterface> MyGenericObject<T> init(Class<T> myParameter) {
   // lots of common code here ... and:
   return new MyGenericObject<T>();
}

The way I use this method at the moment:

MyGenericObject<MyClassA> aInstance = init(MyClassA.class);
MyGenericObject<MyClassB> bInstance = init(MyClassB.class);

Perhaps there must be a way to pass only the type to my method without parameter, but I cannot find the way for it. Could you please help me out with that?


Solution

where I use type of the parameter (T)

But you don't need myParameter to provide you with T. It will work generically without it:

private <T extends MyInterface> MyGenericObject<T> init() {
   // lots of common code here ... and:
   return new MyGenericObject<T>();
}

// and invoke like:
MyGenericObject<MyClassA> aInstance = init();
MyGenericObject<MyClassB> bInstance = init();

A type parameter isn't really a "thing" you use. It's just an instruction to the compiler to make sure that all of the types which refer to T are compatible.



Answered By - Andy Turner
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing