简体   繁体   中英

object created with “new” keyword and created with reflection

I came to know that using Reflection we can create objects without using "new" keyword. So I wanted to know are there any differences in them or any particular scenarios to use Reflection. Because till now i didnt create or seen any of the code creating object with reflection.

Why using 'new' became so used and reflection not.

You'd use reflection only when you want to instantiate a type you based on its name (a string) or a type that you don't have access at compile time (eg a plugin). You wouldn't want to use it in place of new to instantiate types in normal conditions.

Why using 'new' became so used and reflection not.

Compared to the 'new' keyword, using reflection is a lot of work; it also leads to less readable code.

Reflection is used when, for one reason or another, the code that's instantiating the object can't know the concrete type of that object. Reflection is used extensively in creational design patterns .

Reflection is in general much slower and more tedious to use than simple code. It is not a replacement for regular code (like a new operator), but an alternative for situations where regular code can't be used.

Mehrdad is correct. Reflection is used when you need to do something at run-time instead of compile time. Maybe the method you need to execute isn't known until runtime, or the object type isn't. Plug-ins is a good example of this.

Reflection does carry a performance degradation, which is why you should try to limit it only to cases where new won't work for you.

Suppose you want to instanciate the class MyObject using a constructor which takes a single argument of type String . With new :

MyObject myObject = new MyObject("constructor-arg1");

With reflection:

Constructor constructor = MyObject.class.getConstructor(String.class);

MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");

(example stolen from http://tutorials.jenkov.com/java-reflection/constructors.html )

It should be clear than the former is both more readable and faster to type. Moreover reflection is often painfully slow. On the other hand, reflection allows to instanciate objects without knowing their class at compile time.

In conclusion, use reflection only when you have no other alternatives...

In favor of new :

  1. It's a lot faster than reflection.
  2. It leads to much cleaner code, which is easier to maintain.
  3. Reflection is not type-safe.
  4. Because of #3, IDEs with refactoring support (such as Eclipse) cannot handle reflection well.

In favor of reflection:

  1. It's the only option when you don't know the type of the class you want to instantiate at compile time.

So as you can see, new is what you want to use in 99% of cases. Reflection is used inside certain frameworks, like mock object frameworks, dependency injection frameworks, or the service provider interfaces (SPI). In most cases you'll be a user of these things, but not write them yourself.

BTW, if you still want to use reflection, there are tools out there that make instantiating objects through reflection less painful: objenesis comes to mind.

For a more real world example of heavy such usage is in spring framework. In which we will define the dependents of the objects and their collaborating classes in xml and the container will actually create the instances of them using reflection and even call the lifecycle methods using reflection and handover the created object. This is one typical scenario where you will use. Also if you look at most of the BRMS solutions, they use reflection for triggering business rule methods at runtime using reflection. Though the real world every day use of reflection to create objects is low, one other place where you use some kind of reflection is when using JDBC (Class.forName("driver class")).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM