*
The moose likes Beginning Java and the fly likes static constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static constructor" Watch "static constructor" New topic
Author

static constructor

Amin Lakhani
Greenhorn

Joined: Aug 21, 2008
Posts: 1
Why can a constructor not be marked static?

Is it because the constructor needs to know something about the instance it is creating?
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Constructor is meant to set instance variables, it could not access them if it were marked static. Also why would you want to do so?


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi Amin Lakhani welcome to Javaranch ,
As said above if the constructor is static then constructors with parameters will become useless.
Also constructors are not normal methods so making them static will not bind them to the class.
Also static methods are not inherited, so if a constructor becomes static then you will lose all inheritance, in fact you will not be able to construct any object since Object class' constructor will not be called.

And there could be many such reasons.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Here I am assuming that this question is just for the sake of curiosity, otherwise its not logical at all to have a static constructor.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Just few points I want to make ... may be heplful to everyone

1) constructors are never inherrited.
2) static methods can be inherrited.

static keyword can not be associated with constructors because

constructors are called automatically at the time of the creation of the instance of the class.

We dont need any instance of the class to call any static method or static variable.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

static methods can be inherrited.

No they are not inherited. Please seeOverridingVsHiding

constructors are called automatically at the time of the creation of the instance of the class.

We dont need any instance of the class to call any static method or static variable.

I am not getting what you want to say here.

Hope this helps
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
There is a difference between overriding/hiding and inheritence.

Only those instance methods can be overriden which can be inherited.But there can be methods which can not be overriden but can be inherited.

Example:-



In the above example I have accessed display method as "s.display()" This does not mean the instance store in the subclass variable s is invoking static method. Compiler will invoke display method as "parent.display()" only.

One more point .. we know "this" refers to the currently executing instance variable.
Try to access this in any static method .. compiler will not allow this thing.

Hope these point will help you and please let me know if I am wrong somewhere...
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

That's really confusing a bit, but
Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

And
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.

taken directly from Java language specification should make things clear.

Hope this helps
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234




If the above code is an example of inheritance where display method of the parent class is inherited by subclass, then we can inherit static methods.

Note: we can inherit static methods. Static methods are not polymorphic ...

For reference please see "http://www.coderanch.com/t/266357/java-programmer-SCJP/certification/static-method-inherited-overrided "
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Amit, you are talking about static method when they are redefine by child class. In that case static methods are not inherited.

But what if, child class decides not to redefine the static method ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
In that case it remains as a static method of the superclass.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Originally posted by myself:
In that case it remains as a static method of the superclass.


More about static methods in this little thread.#
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by bittoo garg:
Amit, you are talking about static method when they are redefine by child class. In that case static methods are not inherited.

But what if, child class decides not to redefine the static method ?


The JLS clearly says that static methods are not inherited, irrespective of whether it is redefined by the child class or not.

If you carefully read my above post then you can see that it says that the static method is accessible to the child. This is only because of the IS-A relationship fred said in that little thread.
I forgot to mention this IS-A in my last post.
One more thing, I think you are confused with static methods because you are using an object to invoke it.
In your code the "s.display();" is not the proper way to call a static method, seeing code like this one may get an impression that th object s is used to call display() but the fact is s is replaced by subclass before display() can be called.

Hope this helps
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234


You said in previous post that "static method is accessible to the child.This is only because of the IS-A replationship"

My point is IS-A relationship is what we call inheritance which is implemented through extend keyword.


One more thing, I think you are confused with static methods because you are using an object to invoke it.
In your code the "s.display();" is not the proper way to call a static method, seeing code like this one may get an impression that th object s is used to call display() but the fact is s is replaced by subclass before display() can be called.


This thing I have also mentioned in my earlier post "posted Monday, August 25, 2008 3:08 PM"

To ranchers,

Things are getting more complex reagrding static method. As far as my understanding static method can be inherited but can not be overriden .

Ranchers please throw some light on this issue.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi,

Can somebody conclude this thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Originally posted by bittoo garg:
Hi,

Can somebody conclude this thread.
Amit Ghorpade already has. He has told you exactly how static members behave in Java.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by Campbell Ritchie:
Amit Ghorpade already has. He has told you exactly how static members behave in Java.


But looks like bitoo does not agree to it
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi Amit,


If you carefully read my above post then you can see that it says that the static method is accessible to the child. This is only because of the IS-A relationship fred said in that little thread.

This is quoted by you in one of your post.
child class is accessing static method of parent class because of is-a relationship.
My point is :- this is what inheritence is.
I am continuing the topic even when it has been concluded. I hope Ranchers dont mind this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Amit, I suspect you and I have both made the same mistake. If you look in the JLS �6.4.3 it says
The members of a class type (�8.2) are classes (�8.5, �9.5), interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods (�8.4, �9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.8).
So maybe the static methods are in fact inherited after all, but as you correctly say, they cannot be overridden. There are examples in Effective Java and Java Puzzlers (more details on this Ranch page) where there are confusing problems caused by confusion between overriding and hiding static methods.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by Campbell Ritchie:
Amit, I suspect you and I have both made the same mistake.

If there is a mistake at all then the JLS is at fault, as you can see in my post above an excerpt from the JLS says that although static methods are accessible in a sub class, they are not inherited.
So it looks like the JLS is confusing, if not ambiguous.

I am aware of the overriding/hiding confusions. But I'll stick to the not inherited part.
 
Don't get me started about those stupid light bulbs.
 
subject: static constructor
 
Similar Threads
Can a constructor be declared static?
static constructor
Type Constructor & Instance Cnstructor
what is the process if i make a object as static?
Static Constructors???