This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

key word static against constructor

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Why I can not have key word static against the constructor.

thanks
siva
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva
From the JLS 8.8.3:
Unlike methods, a constructor cannot be abstract, static, final, native, strictfp, or synchronized. A constructor is not inherited, so there is no need to declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.

If yout think about it, it makes sense. A static method is a class method is invoked without there having to be an specific object against which it is called. In other words, in an instance method there is a 'this' object that you can use to refer to the object against which the method was invoked. In a static method there is no 'this' because it is a class method not an instance method.
A constructor is more like a special case of an instance method, it has a 'this' reference in it, where 'this' refers to the object that is being created.
Hope that helps
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks You Dave.
-siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic