• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling newInstance() on a class

 
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should be able to use the newInstance() method on any class with a public no-arg constructor, isn't that right?

The following code snippets are taken from an application I'm building. I get the following error:





 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger F. Gay wrote:I should be able to use the newInstance() method on any class with a public no-arg constructor, isn't that right?


No.

newInstance() is a method of class java.lang.Class. So, you need to have an instance of class Class, and you can call newInstance() on that. You can't call it on arbitrary objects.

You probably wanted to do this:

Note that <classname>.class gives you the java.lang.Class object that represents the specified class.

However, why do you want to do this? It's more straightfoward (and more efficient) to just use "new" to create a new object.

 
Roger F. Gay
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I am trying to build something close to javax.xml.stream (StAX) and xmlInputFactory = XMLInputFactory.newInstance(); came from an example that I've used for running StAX. I'm trying to keep everything as close to the current StAX as possible in the hope of using existing javax.xml.stream classes in the future (with a custom xml parser).

Wish I knew how to do that from the start, so I could just write my simple little parser and stick it in there. I'll be using the javax version and a custom parser both. For any other eventual users, I'm hoping to minimize the additional code they'll need.

Doesn't really look like I need to figure out how they performed the miracle that allows XMLInputFactory.newInstance() to work, I guess.
 
Roger F. Gay
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On review - I woke up to the fact that I'm not doing exactly what I said. I'm using code from the javax StAX application that I built; but the source code for StAX that I've been reviewing and partially coping is from the Sun Java Streaming XML Parser (sjsxl) project. For all I know, javax.xml.stream.XMLInputFactory() might have its own newInstance() method.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger F. Gay wrote:For all I know, javax.xml.stream.XMLInputFactory() might have its own newInstance() method.


Yes, it does, as you can see in the API documentation.
 
Roger F. Gay
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I often do, I started the project but am learning as I go - right now spending a lot of time googling and finding out. JAXP, it is said, is a wrapper providing convenience methods that allows one even to use custom parsers.
I even ran across a thread here in which someone explained that you can even use a non-standard parser for pre-processing.
That kind of sounds like just what I'm going for.

But I'm having a terrible time figuring out where to start. Haven't found any examples or tutorials so far ... just keep getting the idea that it's possible to configure parameters in the SAXParserFactory to specify the parser to use.
Any idea on a good approach / resource for focusing right in on that in a concrete and practical manner?
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic