Reputation: 71
Have some questions.
I have a parent class
abstract class Tetramino{}
and some child classes that extends parent with a default constructors listed explicitly
class LineHorizontal extends Tetramino
{
LineHorizontal() { // do some}
}
class LineVertical extends Tetramino
{
LineVertical () {// do some}
}
And I have a generator class which generate child classes
class MinoGenerator
{
private static final String TAG = "MinoGenerator";
private static final Class[] types = {LineHorizontal.class, LineVertical.class, Square.class,
L0.class, L90.class, L180.class, L270.class, LR0.class, LR90.class, LR180.class,
LR270.class, T0.class, T90.class, T180.class, T270.class, Z0.class,
Z180.class, RZ0.class, RZ180.class};
static Tetramino next()
{
try{
int type = (int) (Math.random() * types.length);
return (Tetramino) types[type].newInstance();
} catch (Exception e){
e.printStackTrace();
Log.e(TAG, "Error while creating new tetramino");
}
return null;
}
}
When I want create new one, I call next(). Then a newInstance() function, calls consturtors implicit and return a random one. But android studio mark a constuctors with warning "Constructor neve used", how I can fix it? Thanks for reply.
Upvotes: 2
Views: 6717
Reputation: 140299
A better approach to this, assuming you're using Jack, is to use lambda expressions:
private static final List<Supplier<? extends Tetramino>> types =
Collections.unmodifiableList(
Arrays.asList(
LineHorizontal::new, // or () -> new LineHorizontal()
LineVertical::new,
// etc, for other types
));
Then, in your next()
method:
return types[type].get();
Note that this is actually using the constructor of the class, so you won't have unused warnings any more. And it's more type safe than reflection.
Upvotes: 0
Reputation: 632
Try adding @SuppressWarnings("unused")
hover your class
Like:
@SuppressWarnings("unused")
class MyClass{
public MyClass(){}
}
This works on IntelliJ IDEA and i think that can work also on Android Studio
Upvotes: 2