Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

No Argument Constructor Example

In this section you will learn how to know that whether any class consists No-Argument Constructor or not ?. Here is an example that provides the usage of the newInstance() method in more detail.

No Argument Constructor Example

                         

In this section you will learn how to know that whether any class consists No-Argument Constructor or not ?. Here is an example that provides the usage of the newInstance() method in more detail.

Here in our example we have used "forName()" static method of Class and then we have invoked newInstance() method to create a new object without any argument. Invoking newInstance() method throws a NoSuchMethodException if the class does not have any no-argument constructor.

Here is code for example:

 

 

 

NoArgConstructor.java

import java.lang.reflect.*;
import java.util.ArrayList;

public class NoArgConstructor {
  public static void main(String str[]){
    try{
       ArrayList list = (ArrayList)
       (
Class.forName("java.util.ArrayList").newInstance());
       System.out.println("No-Argument Constructor exist.");
       System.out.println("New Object created Successfully");
    }catch(Exception e){
      System.out.println("No-argument constructor does not exist.");
      System.out.println(e.getMessage());
    }
   }
}

Output:

Download Source code

                         

» View all related tutorials
Related Tags: c class diff io method get vi value field using set if ie example values exam ld e ls can

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.