Q&A Categories

  • No categories

What does static mean ?

Jul

7

2012

What does the static keyword mean?

In: Uncategorized Asked By: aiboogie [228 Blue Star Level]
+2
0
vote
Answer #1

Hi there!

The static keyword marks a variable to be the same for all instances of that class (definition). What does that mean? – That if you are going to create n instances of Student(class) the static variables are going to share the same value throughout all the instances – this is because a static variable is bounded to a memory location, and regardless to any instance it’s going to stay bounded to it. Hence, if you modify the value of a static variable in one instance, the rest of the instances will see it modified.

This brings a great advantage to writing code but also gives the programmer a great tool for adding bugs if not used as it should.

Be aware of these static variables and do use them with caution in multi-threading projects. It’s dangerous to change their values in parallel and then rely on that.

As a good programming practice, the static variables should be referred to using the syntax ClassName”.”variable. They can also be referred to from the instance, but that’s not a good practice because one can misidentify them as instance variables.

The same rule applies to static methods: you do not need an instance in order to be able to call them, and they should as well be called with the same syntax ClassName”.”variable. They can also be referred to from the instance, but that’s not a good practice because one can misidentify them as instance methods.

In general the static methods are utility methods (i.e. string utility methods).

Additional awareness has to be taken into consideration when using the static methods in a multi-threaded environment, since the synchronization lock is put on the Class object and not on an instance, since these methods belong to the class and not to an instance.
B/

Answers Answered By: junior [ Grey Star Level]

 
vote
Answer #2

There are also the static blocks in java to be considered.
http://centraladvisor.com/programming-2/java/static-blocks-in-java/

Answers Answered By: aiboogie [228 Blue Star Level]

Answer this Question

Enter name & email to post an Answer. You can also Login / Join us

For a better management of this site please consider Logging in / Joining us before posting a Question. It makes everithing easier for us.

Please enter the captcha code bellow.
Logged in users don't have to use captchas. Don't have an account? Join us!

 

Link Exchange:

You can add url to the best of the internet directory. Our url is added under Reference Directory
HE Blog Directory WEB LOG SHOWWEB LOG SHOW