Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #5205 > unrolled thread
| Started by | Abu Yahya <abu_yahya@invalid.com> |
|---|---|
| First post | 2011-06-11 22:43 +0530 |
| Last post | 2011-06-14 20:07 +0000 |
| Articles | 7 on this page of 47 — 17 participants |
Back to article view | Back to comp.lang.java.programmer
Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 22:43 +0530
Re: Class.forName().newInstance() vs new David Lamb <dalamb@cs.queensu.ca> - 2011-06-11 13:46 -0400
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-11 19:49 +0200
Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-11 11:04 -0700
Re: Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:52 +0530
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 14:05 +0200
Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-12 07:10 -0700
Re: Class.forName().newInstance() vs new Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-12 14:37 +0000
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 18:17 +0200
Re: Class.forName().newInstance() vs new Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-12 15:25 -0400
Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-15 06:58 -0700
Re: Class.forName().newInstance() vs new Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-06-16 10:16 +0100
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 18:10 +0200
Re: Class.forName().newInstance() vs new Abu Yahya <abu_yahya@invalid.com> - 2011-06-11 23:53 +0530
Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-11 15:43 -0400
Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-11 15:50 -0400
Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-15 06:49 -0700
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-16 11:21 -0400
Re: Class.forName().newInstance() vs new lewbloch <lewbloch@gmail.com> - 2011-06-17 06:45 -0700
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-17 12:29 -0400
Re: Class.forName().newInstance() vs new Lewis Bloch <lewisbloch@google.com> - 2011-06-17 11:07 -0700
Re: Class.forName().newInstance() vs new "John B. Matthews" <nospam@nospam.invalid> - 2011-06-18 02:01 -0400
Re: Class.forName().newInstance() vs new Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-18 10:19 -0300
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-18 20:16 +0200
OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:19 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 20:46 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-21 20:42 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Gene Wirchenko <genew@ocis.net> - 2011-06-21 14:22 -0700
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-20 18:55 -0300
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Tom Anderson <twic@urchin.earth.li> - 2011-06-21 23:29 +0100
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-21 20:39 -0300
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) "John B. Matthews" <nospam@nospam.invalid> - 2011-06-21 23:00 -0400
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Tom Anderson <twic@urchin.earth.li> - 2011-06-22 20:02 +0100
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-22 20:03 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 12:04 -0400
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 11:58 -0400
Re: Class.forName().newInstance() vs new Gene Wirchenko <genew@ocis.net> - 2011-06-20 14:01 -0700
Re: Class.forName().newInstance() vs new Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-20 18:53 -0300
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-21 19:53 -0400
Re: Class.forName().newInstance() vs new blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-22 20:01 +0000
Re: Class.forName().newInstance() vs new Michael Wojcik <mwojcik@newsguy.com> - 2011-06-23 12:16 -0400
OT language stuff (was Re: Class.forName().newInstance() vs new) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:18 +0000
Re: OT language stuff (was Re: Class.forName().newInstance() vs new) Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 20:52 +0000
Re: Class.forName().newInstance() vs new Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 14:14 +0200
Re: Class.forName().newInstance() vs new Patricia Shanahan <pats@acm.org> - 2011-06-11 11:01 -0700
Re: Class.forName().newInstance() vs new Roedy Green <see_website@mindprod.com.invalid> - 2011-06-13 06:16 -0700
Re: Class.forName().newInstance() vs new Ian Shef <invalid@avoiding.spam> - 2011-06-14 20:07 +0000
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Michael Wojcik <mwojcik@newsguy.com> |
|---|---|
| Date | 2011-06-23 12:16 -0400 |
| Message-ID | <itvq8f72avs@news2.newsguy.com> |
| In reply to | #5544 |
blmblm@myrealbox.com wrote: > > Someone should perhaps mention, in this context, George Orwell's essay > "Politics and the English Language" [*]? one of my favorites, which > seems to make a similar connection between language and ethics. Yes. A number of authors and poets at the time - coming out of the Modernist avant-garde period, High Literary Modernism, and the world wars - were both very interested in language and very concerned with its political ramifications. Besides Orwell you have for example Anthony Burgess and Aldous Huxley. The idea that language had political effectivity was rather in the air at the time, fed by psychoanalysis; structural linguistics anthropological theories like the (somewhat misnamed) "Sapir-Whorf Hypothesis"; discussions about art, propaganda, and culture among international Marxists; the work of philosophers such as Heidegger; and so on. But Orwell's is a particularly good example in this area. It's certainly a lot more readable than many of the contemporary philosophical treatments. (I like Derrida just fine, but few people would call _Spectres of Marx_, say, a light read.) -- Michael Wojcik Micro Focus Rhetoric & Writing, Michigan State University
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-06-20 19:18 +0000 |
| Subject | OT language stuff (was Re: Class.forName().newInstance() vs new) |
| Message-ID | <969koeFjduU4@mid.individual.net> |
| In reply to | #5345 |
In article <e43ff9cb-73a9-4d24-8dae-4b0e7ec6d4d1@s41g2000prb.googlegroups.com>,
lewbloch <lewbloch@gmail.com> wrote:
> On Jun 16, 8:21 am, Michael Wojcik <mwoj...@newsguy.com> wrote:
> > lewbloch wrote:
> > > On Jun 11, 12:50 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > >> In article <nospam-BA6E1B.15432711062...@news.aioe.org>,
> > >> "John B. Matthews" <nos...@nospam.invalid> wrote:
> >
> > >>> There's some
> > >> There are some
> >
> > > "There's some [plural] ..." is quite common in idiomatic American.
> >
> > True, which is an argument for avoiding it (or correcting it, as John
> > did in his edit).
> >
> > Though it's not quite as grating as, say, the use of "would" to
> > indicate the subjunctive mood. ("If I would have knowed there's some
> > cupcakes, I would have totally ate them all.")
Sing it.
> > I've pretty much given up on nominative/objective case of pronouns as
> > a shibboleth ("between you and I") - I suspect more people in the US
> > now ignore traditional pronoun case than attempt to apply it -
Me and you agree about that. (I hope I won't regret posting a sentence
I can hardly type without wincing.)
> > but
> > attentive writers do still try to watch agreement in number, I think.
> >
>
> Your comments are appropriate for formal writing. Usenet is
> conversational, ergo one should accept idiomatic expressions.
> Otherwise we'd insist that no one use "doubt" to mean a mere question
> without suspicion.
Oh, but some people do (insist .... ) -- there's been a recent thread
in comp.lang.c in which a few speakers of the non-Indian Englishes
have tried to object to use of "doubt" to mean "question". I doubt
they'll succeed, but -- <shrug>.
> I do aver that one should spell nouns, particularly proper nouns,
> correctly in technical speech, which this is for all its informality,
> and it only makes sense to spell the first-person singular nominative
> pronoun in English correctly as it's short enough to remember.
>
> There's some folks even more pedantic than I, apparently.
*Oh* yes, though some of us aren't quite as vocal about our pedantry.
> Welcome to the club, Michael.
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2011-06-20 20:52 +0000 |
| Subject | Re: OT language stuff (was Re: Class.forName().newInstance() vs new) |
| Message-ID | <itobv7$j1m$2@localhost.localdomain> |
| In reply to | #5426 |
On Mon, 20 Jun 2011 19:18:39 +0000, blmblm@myrealbox.com wrote: > Oh, but some people do (insist .... ) -- there's been a recent thread in > comp.lang.c in which a few speakers of the non-Indian Englishes have > tried to object to use of "doubt" to mean "question". I doubt they'll > succeed, but -- <shrug>. > Indian English is still heavily influenced by Victorian English, especially their conversational forms. Along with railways and the postal system, Victorian english is one of the main legacies of the Raj. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-06-12 14:14 +0200 |
| Message-ID | <95josjFf62U1@mid.individual.net> |
| In reply to | #5221 |
On 06/11/2011 09:43 PM, John B. Matthews wrote:
> In article<it0brg$s3t$2@speranza.aioe.org>,
> Abu Yahya<abu_yahya@invalid.com> wrote:
>
>> Could you give an example of how the factory method might look like?
>
> There's some good examples here:
>
> <http://en.wikipedia.org/wiki/Factory_method_pattern>
>
> And in the linked article, "Item 1: Consider static factory methods
> instead of constructors."
>
> <http://drdobbs.com/java/208403883?pgno=1>
If Class.forName() is the bottleneck then a static factory method does
not help in this particular case. You would have to configure the
method name or a class name anyway and use reflection to get at the
instance or method.
Abu, to answer your question (pseudo code):
interface com.your.company.db.DbManager {
// methods
}
interface com.your.company.db.DbManagerFactory {
DbManager create(/* args if needed */) throws WhateverException
}
class com.your.company.db.ora.OraDbManager implements DbManager {
// ...
}
class com.your.company.db.ora.OracleDbManagerFactory implements
DbManagerFactory {
public DbManager create() {
return new OraDbManager();
}
}
Kind regards
robert
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-06-11 11:01 -0700 |
| Message-ID | <6aadnUFKX6n1MG7QnZ2dnUVZ_uKdnZ2d@earthlink.com> |
| In reply to | #5205 |
On 6/11/2011 10:13 AM, Abu Yahya wrote:
> I have a class that instantiates a private member based on a value in a
> configuration file.
>
> The value is used to determine the class name.
>
> I initially decided to use code similar to the following:
>
>
> // class name comes from property file
> String dbMgrClassName = props.getProperty("db.manager");
> Class<?> k = Class.forName(dbMgrClassName);
> db = (DB) k.newInstance();
> db.init(props);
>
> However, a colleague pointed out that using "new" is faster than using
> "Class.forName().newInstance()". So, I changed the code to the following:
>
> String dbMgrClassName = props.getProperty("db.manager");
> if (dbMgrClassName.equals("DB2"))
> db = new DB2();
> else if (dbMgrClassName.equals("Oracle"))
> db = new Oracle();
> else if (dbMgrClassName.equals("SQLServer"))
> db = new SQLServer();
> else { // handle unrecognized
> Class<?> k = Class.forName(dbMgrClassName);
> db = (DB) k.newInstance();
> }
> db.init(props);
>
> My question is: Does using if-else statements like the above really
> improve performance? What is the best approach to instantiate classes
> whose type you don't know at compile time?
Is performance really a critical issue here? Instantiating a database
manager is usually done quite infrequently, normally once per run, and
is often followed by opening a database, a relatively heavyweight operation.
I could see a case for the second version if you wanted to control the
options for the implementing class. Given the decision to preserve the
old code to handle unrecognized class names it does not even have that
advantage. In order to properly test the second version you will need an
additional class, not "DB2", "Oracle", or "SQLServer", so that the
fall-through code gets exercised.
The first version is simpler and easier to test, so it wins unless it
makes a significant difference in overall program performance.
Patricia
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-06-13 06:16 -0700 |
| Message-ID | <k83cv6trkbcbtu5fdd08q6hvebd9i0hn3i@4ax.com> |
| In reply to | #5205 |
On Sat, 11 Jun 2011 22:43:59 +0530, Abu Yahya <abu_yahya@invalid.com> wrote, quoted or indirectly quoted someone who said : >I have a class that instantiates a private member based on a value in a >configuration file. When you use new, it must be followed by hard coded the name of the class. When you use newInstance, you can create the Class object with Class.forName so all you need is a string containing the class name. This is more general, but more convoluted than using new. When you use Class.forName, GENJAR or the like has no idea that the class need be included in the jar. All it sees is the string, not the class name. The class name does not exist until run time. This can be a good or bad thing. -- Roedy Green Canadian Mind Products http://mindprod.com One of the great annoyances in programming derives from the irregularity of English spelling especially when you have international teams. I want to find a method or variable, but I don't know precisely how its is spelled or worded. English is only approximately phonetic. Letters are randomly doubled. The dictionary often lists variant spellings. British, Canadian and American spellings differ.I would like to see an experiment where variable names were spelled in a simplified English, where there were no double letters.I also think you could add a number of rules about composing variable names so that a variable name for something would be highly predictable. You would also need automated enforcement of the rules as well as possible.
[toc] | [prev] | [next] | [standalone]
| From | Ian Shef <invalid@avoiding.spam> |
|---|---|
| Date | 2011-06-14 20:07 +0000 |
| Message-ID | <Xns9F04859078A43vaj4088ianshef@138.125.254.103> |
| In reply to | #5205 |
Abu Yahya <abu_yahya@invalid.com> wrote in news:it07on$ilc$1
@speranza.aioe.org:
> I have a class that instantiates a private member based on a value in a
> configuration file.
>
> The value is used to determine the class name.
>
> I initially decided to use code similar to the following:
>
>
> // class name comes from property file
> String dbMgrClassName = props.getProperty("db.manager");
> Class<?> k = Class.forName(dbMgrClassName);
> db = (DB) k.newInstance();
> db.init(props);
>
> However, a colleague pointed out that using "new" is faster than using
> "Class.forName().newInstance()". So, I changed the code to the following:
>
> String dbMgrClassName = props.getProperty("db.manager");
> if (dbMgrClassName.equals("DB2"))
> db = new DB2();
> else if (dbMgrClassName.equals("Oracle"))
> db = new Oracle();
> else if (dbMgrClassName.equals("SQLServer"))
> db = new SQLServer();
> else { // handle unrecognized
> Class<?> k = Class.forName(dbMgrClassName);
> db = (DB) k.newInstance();
> }
> db.init(props);
>
> My question is: Does using if-else statements like the above really
> improve performance?
Maybe yes, maybe no. If you don't have a benchmark that shows it to be an
issue, don't worry about it. Initializing the databas manager is probably
going to be a lot more preformance-consuming than anything else that you
are doing here.
> What is the best approach to instantiate classes
> whose type you don't know at compile time?
Well, actually you _do_ know the type for db, you just didn't show us
directly; apparently it is DB. What you don't know at compile time is the
implementation.
I prefer to use java.util.ServiceLoader myself. It means that you have to
have "a provider-configuration file in the resource directory META-
INF/services" instead of a property or a value in a separate configuration
file. This could be a good thing or a bad thing, depending upon your
application.
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.java.programmer
csiph-web