Solved

Class.forName(visitNoteClass).newInstance();

Posted on 2004-09-22
6
784 Views
Last Modified: 2006-11-17
hi!
I need to know what this line does in detail?
Class.forName(visitNoteClass).newInstance();

++++++++++++++++++++++      

      private String visitNoteClass = null;
      private HttpServletRequest request = null;
      private ActionMapping mapping = null;
...
...
            
      }
      public ActionForward execute() {
   
            VisitNoteType v = null;
            Connection conn = null;
            OraConnDispenser ocd = null;
            
            try {
                   v = (VisitNoteType) Class.forName(visitNoteClass).newInstance();            
                   v.init(request);
                   ocd = new OraConnDispenser(null);
                   conn = ocd.getConnection();
...
...
++++++++
thanks,
0
Comment
Question by:dkim18
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 20 total points
ID: 12127072
It creates a new instance of VisitNoteType. 'visitNoteClass' will hold the fully-qulaified classname
0
 
LVL 2

Accepted Solution

by:
SergeiKo earned 40 total points
ID: 12127665
Hello, dkim18.

Look at this short example.


import  java.util.*;

class Test {
    public static void main( String[] args ) {
        // 1
        List list = new ArrayList();
        System.out.println( "list=" + list );

        // 2
        List reflectList = null;
        try {
            reflectList = (ArrayList)Class.forName( "java.util.ArrayList" ).newInstance();
        } catch ( ClassNotFoundException cnfe ) {
            cnfe.printStackTrace();
        } catch ( InstantiationException ie ) {
            ie.printStackTrace();
        } catch ( IllegalAccessException ie ) {
            ie.printStackTrace();
        }
        System.out.println( "reflectList=" + reflectList );
    }
}

Here we do the same thing in two ways.
1. First one is used when we know the class (implementation) before compilation. So we  
    can not to change implementation without recompiling.
2. Second one is used when we know the behaviour and want to change implementation
    without recompiling the program.
    E.g. we can get class name from system properties or command argument and
    instatiate implementation we want:
            reflectList = (ArrayList)Class.forName( arg[0] ).newInstance();

Regards.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 12128879
> Class.forName(visitNoteClass).newInstance();

two things, firstly the call to forName() will return a Class instance for the named class (loading the class if necessary), secondly the newInstance() call will create a new instance of that class calling it's default constructor (no arguments).
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Assisted Solution

by:helloexpert
helloexpert earned 20 total points
ID: 12150722
IMHO
it is not a good idea to create the class instance and instantiate in the same line.. At run time, u wouldnt be able to differentiate if an exception happends in Class.forName or in the newInstance()..

Always,
 do like this
 Class myclass = Class.forName( myclassname);
Object myObj = myclass.newInstance();

this way, in your application, things will be easier to debug, and would improve the readability of hte code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12150784
>>u wouldnt be able to differentiate if an exception happends in Class.forName or in the newInstance()..

Not strictly true, as different subclasses of Excepetion are thrown, although it does make the code more readable/maintainable
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12174505
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question