Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Class.forName(visitNoteClass).newInstance();

Posted on 2004-09-22
6
Medium Priority
?
810 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 80 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 160 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 80 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
Technology Partners: 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 80 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

730 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