Jump to content
  • Advertisement

Archived

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

Lucidquiet

[java] Static and final.

This topic is 5350 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
Advertisement
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:

a final variable: value can only be set by constructor and cannot be changed later on



Or it can be initialized at the point of declaration.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!