Hello.

It is necessary to create a method that would accept any copy of the object!

Is it possible to write a method that would accept and process any type it receives at compile time?

public static void main (String [] strings){ inspector(new User("Kostia","Kostia000","Kostia..Kostikrus90@gmail.com")); inspector(new Message("","")); // And more beans } public static void inspector(Object object) { if(!(object.equals(null))){ if(object instanceof User){ // Make only one method and one check for instance to all incoming type of object instances! Converter<User> typeConverter = new Converter<User>(); User user = typeConverter.convertTo(object); System.out.print(user.toString()); } } } public class Converter<T> { protected T convertTo(Object type){ return (T) type; } } 
  • The phrase “ any instance of an object ” is incorrect. An object is an instance of a particular type. You obviously meant "an object of an arbitrary type ." - post_zeew
  • And what's the point, what's the point? ;) - barmaglott
  • Learning .. familiarization with generics .. - Maks.Burkov

1 answer 1

How to create a Generic method for type checking?

For example:

 public <T> boolean compareTypes(T firstObject, T secondObject) { return firstObject.getClass().equals(secondObject.getClass()); }