Solved

Class.forName(visitNoteClass).newInstance();

Posted on 2004-09-22
6
768 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Assisted Solution

by:helloexpert
helloexpert earned 20 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
:-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

763 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

10 Experts available now in Live!

Get 1:1 Help Now