Difference between "new classname()" and  class.forName("...")?

Hi,
Please clarify me : What is the difference between the instnce creation by using new operator *( new ClassName())* and instance creation using class.forName("fully Qualified Name");
thanks
Gaurav

The compiler can tell you whether new ClassName() will be valid. For the other, you have to wait until runtime to know for sure.
new ClassName(...) can be invoked with arguments, and you provide them directly and simply. If you want to reflectively create an instance using a constructor that takes arguments, it's a more convoluted process.
Using new, you know which checked exceptions can be thrown and you catch them directly. Using reflection, you have to catch some other exception and unravel its cause.
You can use reflection (Class.forName and its more complicated cousins) when you don't know the classname at compile time.You can't do this with new.
Don't use reflection unless you have to. It's more complex, somewhat less performant, and it denies you certain compile time checks.

Similar Messages

  • Difference between DriverManager.registerDriver and Class.forName?

    Hi all,
    I've noticed that the Oracle JDBC driver specifically asks to use DriverManager.registerDriver instead of Class.forName. What is the difference btw those 2 methods in loading and registering the driver?
    Thanks.

    I am trying to understand the jdbc, odbc drivers better.
    Does the class my.sql.Driver refer to a driver for MySql or is it a driver itself?
    with the lines one and two commented out, the code works fine. but with lines one and three commented out and the dbDriver being as specified in line two; no connection is made to the database. Can anyone explain this? and when i do Class.forName(dbDriver), is there a class by that name?
    //String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; //one
    //String dbDriver = "my.sql.Driver"; //two
    String dbDriver = "MySql ODBC 3.51 Driver"; //three
    String dbSource = "jdbc:odbc:mydb";
    try {
         Class.forName(dbDriver);
         conn = DriverManager.getConnection(dbSource);
         statement = conn.createStatement();
    } catch(Exception exception) {
         System.out.println(exception.getMessage()+" from the "+exception.toString());
         System.exit(1);

  • What is the Difference between ?  new Operator and Class.forName() ???

    plz tel me ....
    what is the Difference between ? new Operator and Class.forName() ??? ........

    Class.forName(), takes the class name as parameter,
    and loads that class in memory. But it doesn't create
    any instance of that class.
    That means static methods/variables are available for
    use.
    new keyword, checks if the class is loaded, if not
    then loads that class, and then creates an instance
    of that class.Class.forName actually returns the class object for that name (class - for -name). it might load it, if the class hasn't already been loaded, but it's misleading to say that's what that method does
    your definition of 'new' is wrong, too. give the dukes back

  • Difference Between Business Object And Class Object

    Hi all,
    Can any one tel me the difference between business object and class Object....
    Thanks...
    ..Ashish

    Hello Ashish
    A business object is a sematic term whereas a class (object) is a technical term.
    Business objects are all important objects within R/3 e.g. sales order, customer, invoice, etc.
    The business objects are defined in the BOR (transaction SWO1). The have so-called "methods" like
    BusinessObject.Create
    BusinessObject.GetDetail
    BusinessObject.Change
    which are implemented (usually) by BAPIs, e.g.:
    Business Object = User
    User.Create => BAPI_USER_CREATE1
    User.GetDetail => BAPI_USER_GET_DETAIL
    CONCLUSION: Business Object >< Class (Object)
    Regards
      Uwe

  • Difference between New Formula and Formula Variable

    Can any body explain what is the difference between  New Formula and Formula Variable.
    Thanks

    Hi,
    Formula:http://help.sap.com/saphelp_nw04/helpdata/en/d3/8057f830a911d4b2be0050dadfb23f/frameset.htm
    Formula Variable:If you want use variables in the formulas of a form, you need to use formula variables. You can use formula variables only in form reports, and must define them in the form itself. They can be replaced in the same ways as text variables when you execute the report.
    http://help.sap.com/saphelp_nw04/helpdata/en/5c/8db0b1555411d189660000e829fbbd/frameset.htm
    Bye
    Dinesh

  • Difference between interface pool and class pool

    Hi,
    Can any body tell me the difference between Interface pool and Class pool.
    thank you in advance.
    regards

    Hi,
    Class and Interface Pools
    This section discusses the structure and special features of class and interface pools for global classes.
    Global Classes and Interfaces
    Classes and interfaces are object types. You can define them either globally in the Repository or locally in an ABAP program. If you define classes and interfaces globally, special ABAP programs called class pools or interface pools of type K or J serve as containers for the respective classes and interfaces. Each class or interface pool contains the definition of a single class or interface. The programs are automatically generated by the Class Builder when you create a class or interface.
    A class pool is comparable to a module pool or function group. It contains both declarative and executable ABAP statements, but cannot be started on its own. The runtime system can create runtime instances (objects) through a request using the CREATE OBJECT statement. These objects execute the statements of the class pool.
    Interface pools do not contain any executable statements. Instead, they are used as containers for interface definitions. When you implement an interface in a class, the interface definition is implicitly included in the class definition.
    Structure of a Class Pool
    Class pools are structured as follows:
    Class pools contain a definition part for type declarations, and the declaration and implementation parts of the class.
    Differences From Other ABAP Programs
    Class pools are different from other ABAP programs for the following reasons:
    ·        ABAP programs such as executable programs, module pools, or function modules, usually have a declaration part in which the global data for the program is defined. This data is visible in all of the processing blocks in the program. Class pools, on the other hand, have a definition part in which you can define data and object types, but no data objects or field symbols. The types that you define in a class pool are only visible in the implementation part of the global class.
    ·        The only processing blocks that you can use are the declaration part and implementation part of the global class. The implementation part may only implement the methods declared in the global class. You cannot use any of the other ABAP processing blocks (dialog modules, event blocks, subroutines, function modules).
    ·        The processing blocks of class pools are not controlled by the ABAP runtime environment. No events occur, and you cannot call any dialog modules or procedures. Class pools serve exclusively for class programming. You can only access the data and functions of a class using its interface.
    ·        Since events and dialog modules are not permitted in classes, you cannot process screens in classes. You cannot program lists and selection screens in classes, since they cannot react to the appropriate events. It is intended to make screens available in classes. Instead of dialog modules, it will be possible to call methods of the class from the screen flow logic.
    Local Classes in Class Pools
    The classes and interfaces that you define in the definition part of a class pool are not visible externally. Within the class pool, they have a similar function to local classes and interfaces in other ABAP programs. Local classes can only be instantiated in the methods of the global class. Since subroutines are not allowed in class pools, local classes are the only possible modularization unit in global classes. Local classes have roughly the same function for global classes as subroutines in function groups, but with the significant exception that they are not visible externally
    Reward points if useful....
    Regards
    AK

  • Difference between New Archieve and New Patch

    Hi,
    I am going through import and export topics of LCA files (http://help.adobe.com/en_US/livecycle/9.0/workbenchHelp/help.htm?content=000276.html ). I understood how we can create new Archieves and import it to another environments. But I am not able to understand about patches.
    In both the cases, I am doing the same thing. I am creating archieve of the application. I am sure, there would be difference between "new archive" and "Patch" but not able to figure out.
    Can anyone let me know if I create new archieve and Patch, then what is the visual differences i can look in both the archieves??????
    Regards
    Sunil

    patching is related to component of LiveCycle.
    these are  basically jar files updated with new one. For more please look here.
    http://help.adobe.com/en_US/livecycle/9.0/workbenchHelp/help.htm?content=000960.html
    LCA files are simply archive file of the processes/application that you create in workbench. These archive files can be import to other environment  at later stage.
    -Vijay

  • Difference between new network and extended network

    Difference between extended network and new network settings

    An "extended" network acts as one large wireless network. Wireless devices can roam anywhere a signal is present and stay on the network without having to make any changes.
    A "new" network will require that a wireless device manually "switch" to that network and enter the password for the network to connect whenever you want to use that network. In other words, a "new" network will use a different wireless network name and password, which will require that you manually log on to that network.
    The exception would be if you created a "new" wireless network and used the same wireless network name, same wireless security settings and same password as the "main" network and connected the AirPort back to the main router using an Ethernet cable. In that case, you would have an "Ethernet extended wireless" network.

  • Difference between using FM and class in alv

    does anyone know what's the main difference between using FM to create alv  and using class to create alv?
    in what condition we usually use FM or class?

    using FM  we can create Classical and Interactive ALV Reports.
    The classes are used to create the ALV Reports with OOPS concept.
    Check this for basic concepts of OOPS
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/b6cae890-0201-0010-ef8b-f970a9c41d47
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/1591ec90-0201-0010-3ba8-cdcd500b17cf
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap%20code%20samples/alv%20grid/abap%20code%20sample%20to%20display%20data%20in%20alv%20grid%20using%20object%20oriented%20programming.doc
    Tabstrip
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/022ba607-0301-0010-e382-fdedca553f5f
    Editable ALV
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/ec31e990-0201-0010-f4b6-c02d876ce033
    Tree
    http://www.sapdevelopment.co.uk/reporting/alv/alvtree/alvtree_usrint.htm
    General Tutorial for OOPS
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/e8a1d690-0201-0010-b7ad-d9719a415907
    Reward Points if useful

  • What is the difference between new thread and new execution?

    What is the difference in creating a new thread and a new execution. Basically, what is the difference between thread and execution?

    Try this link...
    http://zone.ni.com/devzone/cda/tut/p/id/4823
    Thanks,
    PH

  • What is the difference between instance variable and class variable?

    i've looked it up on a few pages and i'm still struggling a bit maybe one of you guys could "dumb" it down for me or give and example of how their uses differ.
    thanks a lot

    Instance is variable, class is an object.What? "Instance" doesn't necessarily refer to variables, and although it's true that instances of Class are objects, it's not clear if that's what you meant, or how it's relevant.
    if you declare one instance in a class that instance
    should be sharing within that class only.Sharing with what? Non-static fields are not shared at all. Sharing which instance? What are you talking about?
    if you declare one class object it will share
    anywhere in the current program.Err...you can create global variables in Java, more or less, by making them static fields. If that's what you meant. It's not a very good practice though.
    Static fields, methods, and classes are not necessarily object types. One can have a static primitive field.

  • Difference between New ETL and Old ETL

    Hi All,
    In old ETL process we first have a InfoSource (Can be connected from DataSources from Different Source Systems). We have 1 set of Transfer Rules. Again if there are different definitions to be loaded into different data target we define them in Update Rules.
    What incase of new ETL is the purpose of maintaining 2 transformations (Replacing Update Rules and Tranfer Rules). Can't we directly define different transformations on DataSource itself. Where the requirement comes that we define two transformations with InfoSource in between.

    Hi Ravi,
    What incase of new ETL is the purpose of maintaining 2 transformations (Replacing Update Rules and Tranfer Rules). Can't we directly define different transformations on DataSource itself. Where the requirement comes that we define two transformations with InfoSource in between.
    .here is the requirement
    suppose thr are some modification required to be done in an infoobject value ..suppose the value '1' is to be replaced by 'Y'  in an infoobject for all data targets...then such a modification will be put in the transformation from datasource to infosource and after that individual transformations are applied from infosource to different data targets,these transformation are data target specific rules..like update rules..in this case u only had to write the rule for 1-->Y once in first transformation ...
    So in short .. transfer rules as well as update rules are now called transformations.....
    yes u can directly define transformations on datasource itself as well ..infosource is not mandatory
    Mann

  • Can't see difference between new posts and old ones.

    Hi
    This may sound like a silly question......but why I can't see new posts everything looks the same, no indication of new posts.
    It's very silly not to be able to see new posts.
    Tony

    Agreed guys ! We risk to not answer to someone, because we don't see that the thread has been updated. Before it was very easy to do that, now it' quite difficult :-(
    Paul

  • Difference between new GL & Classic GL

    Hi
    I request everyone to help me understand the difference between new GL and Classic GL.
    Basically a new GL has following features:-
    1. Segmental Reporting
    2. Parallel Currencies
    3. Generation of Balance Sheets @ Profit Center Level
    4. Legal Consolidation
    I believe all the above features can also be achieved in Classic GL.
    Also what is the difference between Business Area & Segments?
    Pls help.
    Thanks,
    Srikanth

    Hi Srikanth,
    One of the the core differnences between New GL and Glassic Gl is online integration of  items like PCA,segments,Business area etc with GL.
    In classic GL we need to do a lot of periodic closing activities to get restricted Balance sheet at these levels.
    with online document split enabled in New gl these periodic activities are gone.
    Differnce between business areas and segments:
    both can be used to draw financial statements at other than co.code level. But for segmental reporting you need to have PCA as segments can be derived only thru Profit center. (BADI available in other cases)whereas Business areas are cross co.code settings so you can assign them to sales area/plant/divsion combination for derivation which remains the same as classic gl.
    Hope this helps.
    Pls assign points if it does.
    Thanks,
    vamsi
    Edited by: Vamsi on Jan 21, 2008 7:06 AM

  • Question about main difference between Java bean and Java class in JSP

    Hi All,
    I am new to Java Bean and wonder what is the main difference to use a Bean or an Object in the jsp. I have search on the forum and find some post also asking the question but still answer my doubt. Indeed, what is the real advantage of using bean in jsp.
    Let me give an example to illustrate my question:
    <code>
    <%@ page errorPage="errorpage.jsp" %>
    <%@ page import="ShoppingCart" %>
    <!-- Instantiate the Counter bean with an id of "counter" -->
    <jsp:useBean id="cart" scope="session" class="ShoppingCart" />
    <html>
    <head><title>Shopping Cart</title></head>
    <body bgcolor="#FFFFFF">
    Your cart's ID is: <%=cart.getId()%>.
    </body>
    <html>
    </code>
    In the above code, I can also create a object of ShoppingCart by new operator then get the id at the following way.
    <code>
    <%
    ShoppingCart cart = new ShoppingCart();
    out.println(cart.getId());
    %>
    </code>
    Now my question is what is the difference between the two method? As in my mind, a normal class can also have it setter and getter methods for its properties. But someone may say that, there is a scope="session", which can be declared in an normal object. It may be a point but it can be easily solved but putting the object in session by "session.setAttribute("cart", cart)".
    I have been searching on this issue on the internet for a long time and most of them just say someting like "persistance of state", "bean follow some conventions of naming", "bean must implement ser" and so on. All of above can be solved by other means, for example, a normal class can also follow the convention. I am really get confused with it, and really want to know what is the main point(s) of using the java bean.
    Any help will be highly apprecaited. Thanks!!!
    Best Regards,
    Alex

    Hi All,
    I am new to Java Bean and wonder what is the main
    difference to use a Bean or an Object in the jsp. The first thing to realize is that JavaBeans are just Plain Old Java Objects (POJOs) that follow a specific set of semantics (get/set methods, etc...). So what is the difference between a Bean and an Object? Nothing.
    <jsp:useBean id="cart" scope="session" class="ShoppingCart" />
    In the above code, I can also create a object of
    ShoppingCart by new operator then get the id at the
    following way.
    ShoppingCart cart = new ShoppingCart();
    out.println(cart.getId());
    ...Sure you could. And if the Cart was in a package (it has to be) you also need to put an import statement in. Oh, and to make sure the object is accessable in the same scope, you have to put it into the PageContext scope. And to totally equal, you first check to see if that object already exists in scope. So to get the equivalant of this:
    <jsp:useBean id="cart" class="my.pack.ShoppingCart"/>Then your scriptlet looks like this:
    <%@ page import="my.pack.ShoppingCart %>
    <%
      ShoppingCart cart = pageContext.getAttribute("cart");
      if (cart == null) {
        cart = new ShoppingCart();
        pageContext.setAttribute("cart", cart);
    %>So it is a lot more work.
    As in my mind, a normal class can also
    have it setter and getter methods for its properties.True ... See below.
    But someone may say that, there is a scope="session",
    which can be declared in an normal object.As long as the object is serializeable, yes.
    It may be
    a point but it can be easily solved but putting the
    object in session by "session.setAttribute("cart",
    cart)".Possible, but if the object isn't serializable it can be unsafe. As the point I mentioned above, the useBean tag allows you to check if the bean exists already, and use that, or make a new one if it does not yet exist in one line. A lot easier than the code you need to use otherwise.
    I have been searching on this issue on the internet
    for a long time and most of them just say someting
    like "persistance of state", "bean follow some
    conventions of naming", "bean must implement ser" and
    so on. Right, that would go along the lines of the definition of what a JavaBean is.
    All of above can be solved by other means, for
    example, a normal class can also follow the
    convention. And if it does - then it is a JavaBean! A JavaBean is any Object whose class definition would include all of the following:
    1) A public, no-argument constructor
    2) Implements Serializeable
    3) Properties are revealed through public mutator methods (void return type, start with 'set' have a single Object parameter list) and public accessor methods (Object return type, void parameter list, begin with 'get').
    4) Contain any necessary event handling methods. Depending on the purpose of the bean, you may include event handlers for when the properties change.
    I am really get confused with it, and
    really want to know what is the main point(s) of
    using the java bean.JavaBeans are normal objects that follow these conventions. Because they do, then you can access them through simplified means. For example, One way of having an object in session that contains data I want to print our might be:
    <%@ page import="my.pack.ShoppingCart %>
    <%
      ShoppingCart cart = session.getAttribute("cart");
      if (cart == null) {
        cart = new ShoppingCart();
        session.setAttribute("cart", cart);
    %>Then later where I want to print a total:
    <% out.print(cart.getTotal() %>Or, if the cart is a JavaBean I could do this:
    <jsp:useBean id="cart" class="my.pack.ShoppingCart" scope="session"/>
    Then later on:
    <jsp:getProperty name="cart" property="total"/>
    Or perhaps I want to set some properties on the object that I get off of the URL's parameter group. I could do this:
    <%
      ShoppingCart cart = session.getAttribute("cart");
      if (cart == null) {
        cart = new ShoppingCart();
        cart.setCreditCard(request.getParameter("creditCard"));
        cart.setFirstName(request.getParameter("firstName"));
        cart.setLastName(request.getParameter("lastName"));
        cart.setBillingAddress1(request.getParameter("billingAddress1"));
        cart.setBillingAddress2(request.getParameter("billingAddress2"));
        cart.setZipCode(request.getParameter("zipCode"));
        cart.setRegion(request.getParameter("region"));
        cart.setCountry(request.getParameter("country"));
        pageContext.setAttribute("cart", cart);
        session.setAttribute("cart", cart);
      }Or you could use:
    <jsp:useBean id="cart" class="my.pack.ShoppingCart" scope="session">
      <jsp:setProperty name="cart" property="*"/>
    </jsp:useBean>The second seems easier to me.
    It also allows you to use your objects in more varied cases - for example, JSTL (the standard tag libraries) and EL (expression language) only work with JavaBeans (objects that follow the JavaBeans conventions) because they expect objects to have the no-arg constuctor, and properties accessed/changed via getXXX and setXXX methods.
    >
    Any help will be highly apprecaited. Thanks!!!
    Best Regards,
    Alex

Maybe you are looking for