Solved

Class.forName(visitNoteClass).newInstance();

Posted on 2004-09-22
6
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

627 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