• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Constructors???

 
Dibbo Khan
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Dibbo Khan
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"otherwise perfect language"

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

Here's the Amazon Listing
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it Static Initializers that we're talking about. It is invoked even before the constructor of a class.
 
Dibbo Khan
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static constructors are used for class initialyzing . The code how i see it :
class Example
{
static
{
//static constructor of the class
}
{
}
Example(){//class constructor}
}

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic