Returns an instance of the given class name, by calling the default constructor. : Class Method Field Name « Reflection « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Reflection » Class Method Field NameScreenshots 
Returns an instance of the given class name, by calling the default constructor.
 
/*
 * $Id: ClassUtil.java 709153 2008-10-30 12:54:10Z apetrelli $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */



/**
 * Utilities to work with dynamic class loading and instantiation.
 *
 @version $Rev: 709153 $ $Date: 2008-10-30 13:54:10 +0100 (Thu, 30 Oct 2008) $
 @since 2.0.7
 */
public class Main {

  /**
   * Returns an instance of the given class name, by calling the default
   * constructor.
   *
   @param className The class name to load and to instantiate.
   @param returnNull If <code>true</code>, if the class is not found it
   * returns <code>true</code>, otherwise it throws a
   * <code>TilesException</code>.
   @return The new instance of the class name.
   @throws CannotInstantiateObjectException If something goes wrong during instantiation.
   @since 2.0.7
   */
  public static Object instantiate(String className, boolean returnNull) {
      ClassLoader original = Thread.currentThread().getContextClassLoader();
      if (original == null) {
          Thread.currentThread().setContextClassLoader(Main.class.getClassLoader());
      }
      try {
          Class<?> namedClass = Class.forName(className);
          return namedClass.newInstance();
      catch (ClassNotFoundException e) {
          if (returnNull) {
              return null;
          }
          throw new RuntimeException(
                  "Unable to resolve factory class: '" + className + "'", e);
      catch (IllegalAccessException e) {
          throw new RuntimeException(
                  "Unable to access factory class: '" + className + "'", e);
      catch (InstantiationException e) {
          throw new RuntimeException(
                  "Unable to instantiate factory class: '"
                          + className
                          "'. Make sure that this class has a default constructor",
                  e);
      finally {
          // If the original context classloader of the current thread was
          // null, it must be reset.
          if (original == null) {
              Thread.currentThread().setContextClassLoader(null);
          }
      }
  }

}

   
  
Related examples in the same category
1. Create a new instance given a class name
2. Load a class given its name.
3. Get the short name of the specified class by striping off the package name.
4. Get non Package Qualified Name
5. Returns the name of a class without the package name
6. Returns the package portion of the specified class
7. Format a string buffer containing the Class, Interfaces, CodeSource, and ClassLoader information for the given object clazz.
8. Create a unique hash for Constructor and method
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.