[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

new Something() verses Class.forName("Something")

Posted on 2005-04-13
5
Medium Priority
?
236 Views
Last Modified: 2010-03-31
I see Class.forName() used for it's "side effect", which
is to load a class.  What's the difference, in that respect,
from just creating, with new, an instance of that class?  

new Something();

verses

Class.forName("Something");


Thanks
0
Comment
Question by:hank1
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13770571
forName() does not create a new instance of the class, it only loads the class and creates the Class instance
0
 
LVL 1

Author Comment

by:hank1
ID: 13770604
Why would you want to? In the book they load the class
to initialize hardware.  Is loading a class like this more
efficient?  What's the difference in loading a class verses
creating an instance.  They seem very much alike to me.

Again, why would you do this.  Why just load a class?
0
 
LVL 92

Expert Comment

by:objects
ID: 13770650
Various reasons, mostly if you don't actually know the type of class you want to create at compile time. For example if you read it from a config file.

> Why just load a class?

Typically you wouldn't be just loading a class, that class would be being used (and instances of it created) later in the code.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 200 total points
ID: 13770674
A new instance will be created if you call newInstance ().

>> Why would you want to?

To remove hard-coding. Let's say that you have a Base class or an interface:

public interface IDao // for data-access
{
  public void updateStatus ( int value ) ;
  // etc
}

Let's say that you want to provide two different implementations of this interface - one which writes to an Oracle data-base, and one which writes to a SQL-Server data-base. Let's say the classes are:

public class OracleDao implements IDao // for Oracle data-access

public class SQLServerDao implements IDao // for SQL-Server data-access

- which define implementations of those methods in their own native formats. Now, for one installation, if you want to use Oracle data-base and for another one, you want to use SQL Server - how will you do it if you have hard-coded 'new OracleDao ()' or 'new SQLServerDao ()' in the code?

To overcome that, you can use an IDao reference in your code:

IDao dataAccessObj ;

- and instead of doing: dataAccessObj = new OracleDao () ; or new SQLServerDao () ;, you could use:

dataAccessObj = ( IDao ) Class.forName ( theClassToUse ).newInstance () ;

The value of 'theClassToUse' can be obtained from a properties-file or something, hence you can change that for multiple runs of your application without changing the code.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13770705
You would still use the interface-reference for invoking methods, like:

dataAccessObj.updateStatus ( value ) ;

- and calls would result on the actual object it is referring to (instance of OracleDao or SQLServerDao, based on whatever you loaded and created an instance of).
0

Featured Post

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!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

873 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