Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reflections/default-constructor issue

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
0
0
US
Hi,

Unless I'm mistaken, default constructors (i.e. no arguments) are automatically provided for a class if you opt not to explicitly provide one yourself.

So, I'm contending with the following:
Code:
class MyClass
{
}

MyClass Fubar = new MyClass();

Constructor ct = ( Fubar.getClass() ).getConstructor(null);

When I define a default constructor (i.e. public MyClass(){} )
for class MyClass, this all works fine. However, when I don't define this default constructor, a "NoSuchMethodException" is generated. Why would this be, if a default constructor is provided for me??

Thanks for any/all help!
dora
 
How interesting. Yes, Java provides default constructors but I suspect Java does a special and actually makes the default constructor as required if it cannot find another constructor. getConstructor() only displays real constructors.

Not much help really...

scrat
 
I've always been skeptical about the existence of default constructors.

Since they do nothing and return nothing, how can you tell if they exist? If you were writing a JVM, would you put in special code to generate a constructor that did nothing? Wouldn't such a method get optimized right back out again? In a language that has struggled to achieve acceptable performance speed, it would seem expendable.

I think Dora has discovered the truth. I guess you catch the exception and ignore it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top