Solved

Class.forName(visitNoteClass).newInstance();

Posted on 2004-09-22
6
774 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 130
firstChar challenge 13 106
VB Script to add site to Java Exception List 4 48
arguments to jar 5 7
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now