• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Constructor in Java

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

From Thinking in Java book

in java, constructors are static implicitly

the above sentence confused me and I dont find depth discussion on this.
as far I know, you can only apply access specifier to constructor and you cant apply static.

any help?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:as far I know, you can only apply access specifier to constructor and you cant apply static.

That's what "implicit" means--it means you don't have to specify it (and as you said, you can't). The "static" is implied.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote: The "static" is implied.

well David, but still you can use instance variable and *this* inside constructor which is not a static and you cant use these in static block right?.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors aren't methods.

Think of it this way: An instance of a class has methods (non-static methods). A class has methods (static methods). A class has constructors (implicitly static--what would it mean for an instance to have a constructor?)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it simply means the constructors belong to the class rather than to instances. But they set up instance fields.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a clearer way of putting it, I think.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you David and Campbell. I understood clearly. it is clever explanation. once again thank you
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any specific property for a variable defined inside Static Block.Static means when the class is loaded all the static members are called only once and initialised. So whenever we create some instance variable inside static , is there any change in property for that variable.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking if you can change the value of a property initialized inside a static initializer? Why not just try it?
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried changing properties. But my Question is , Is there any difference in properties for an instance created inside a class normally and an instance created inside a Static initialiser. By meaning property , i here mean whether conceptually is there any difference between those, it's accessibility,Scope,etc. Thanks David.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic