my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Constructors??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Constructors???" Watch "Static Constructors???" New topic
Author

Static Constructors???

Dibbo Khan
Ranch Hand

Joined: Dec 19, 2004
Posts: 147
Is it illegal to have a static constructor, I think it most probably is???

Not a static method that returns an instance, but a static constructor???


MCPD (Enterprise Application Developer, Windows Developer, Web Developer - .NET 2.0), MCTS (Windows Apps, Web Apps and Disbributed Applications - .NET 2.0), MCITP (Database Developer & Business Intelligence Developer - SQL Server 2005), MCAD, MSCD.net, SCJP 5, SCWCD 1.4, SCBCD, SCMAD, SCDJWS, SCJA
Dibbo Khan
Ranch Hand

Joined: Dec 19, 2004
Posts: 147
Sorry to waste everyone's time.

I just tested this on Netbeans 4.1 and realise this is fully illegal. I thought this was a stupid question, but I didn't know if by some quirk of the java language this is legal.

I personally hate two things about an otherwise perfect language, initialization blocks and co-variant return types.
I think they make code look very messy.

Dibbo
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"otherwise perfect language"

Um... treat yourself to a copy of "Java Puzzlers", it's a great eyeopener.

Here's the Amazon Listing


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
By the way, you can have static so called factory methods which create objects (using constructors) and return references to them. Usually the type returned by the factory method is an interface implemented by the class being instantiated.
Dibbo Khan
Ranch Hand

Joined: Dec 19, 2004
Posts: 147
I have heard of that book.

I was just saying Otherwise Perfect so that everyone wouldn't jump on me and verbally assault me.

I actually program solely in C# but am very interested in Java and like the language for its elegance overall.

Do you recommend the book, Java Puzzles, does it give you a good insight into the unanticipated behavior of the language???

Dibbo
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Dibbo Khan:

Do you recommend the book, Java Puzzles, does it give you a good insight into the unanticipated behavior of the language???

Dibbo


Yes, very much so. If you browse through out Java In General (Intermediate) forum you will be able to read some posts from Joshua Bloch one of the authors.

PS. This is not an ad. I have bought the book and have it in my Safari collection.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
Is it Static Initializers that we're talking about. It is invoked even before the constructor of a class.


I'm a Hood Ornament
Dibbo Khan
Ranch Hand

Joined: Dec 19, 2004
Posts: 147
No static constructors, I found an example from a practice test question in the Sun E practice exams.

But I now clearly know, and always should have that static constructors are illegal.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

static constructors are illegal.
I think it's also important to remember that constructors are not members of a class, and have other special rules (they're not inherited, there is no return type, etc.).
Radu Mantale
Greenhorn

Joined: Aug 29, 2005
Posts: 4
Static constructors are used for class initialyzing . The code how i see it :
class Example
{
static
{
//static constructor of the class
}
{
}
Example(){//class constructor}
}

 
 
subject: Static Constructors???
 
Similar Threads
Can a constructor be declared static?
static constructor
Type Constructor & Instance Cnstructor
return; not allowed in initializers
what is the process if i make a object as static?