Archived

This topic is now archived and is closed to further replies.

[java] Static and final.

This topic is 5046 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m suprised I have not asked this question before, but what is the difference between these two modifiers: static and final ( I can see a few but hoping for a more informed comment.) Also what are the consequences of declaring a class as final? I''ve never done it before. So, when I started writing a class filled with methods I later wanted to make all static it sort of dawned on me that maybe I could just declare the class static, except that it didn''t work, of course. (Besides I was using the this pointer in a few places.) To be honest I can''t think of what the deal might be for making something like a class final --something like const is ringing a bell, but I don''t know. Allong the same lines if all the methods (like math class) are going to be static wouldn''t it be allowable to make a class static so as not to have to worry about accidently ''not'' declaring a method as static.

Share this post


Link to post
Share on other sites
a static variable: can be accessed without creating an instance of the class
a final variable: value can only be set by constructor and cannot be changed later on

a static final variable: can be accessed without creating an instance of the class and the value cannot be changed. (i.e Math.Pi)

a final class: cannot be subclassed

Share this post


Link to post
Share on other sites
quote:
Original post by usser
a static variable: can be accessed without creating an instance of the class
a final variable: value can only be set by constructor and cannot be changed later on

a static final variable: can be accessed without creating an instance of the class and the value cannot be changed. (i.e Math.Pi)

a final class: cannot be subclassed


Alright its all coming back to me now. Thanks.

quote:
Original post by pinacolada
If you were able to make the class static, then a baby OO angel would lose its wings.


Haha... yep no joke.

Share this post


Link to post
Share on other sites