• 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
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

static constructor

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can a constructor not be marked static?

Is it because the constructor needs to know something about the instance it is creating?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




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 "https://coderanch.com/t/266357/java-programmer-SCJP/certification/static-method-inherited-overrided "
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Marshal
Posts: 79670
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case it remains as a static method of the superclass.
 
Campbell Ritchie
Marshal
Posts: 79670
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can somebody conclude this thread.
 
Campbell Ritchie
Marshal
Posts: 79670
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79670
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
I want my playground back. Here, I'll give you this tiny ad for it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic