• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Static Constructors???

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.).
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}
}

 
"To do good, you actually have to do something." -- Yvon Chouinard
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic