Satic and Final in Java

Static and final in Java

  • A static method should be called using the class name rather than object reference variables.
  • A static method can be invoked without any instances of the method’s class on the heap.
  • A static method is good for a utility method that does not depend on a particular instance variable value.
  • A static method is not associated with a particular instance – only the class = so it cannot access any instance variable values of its class.  It wouldn’t know which instance’s values to use.
  • A static method cannot access a non-static method, since non-static methods are usually associated with instance variable state.
  • If you have a class with only static methods and you do not want the class to be instantiated you can mark the constructor private.
  • A static variable is a variable shared by all members of a given class.  There is only one copy of a static variable in a class, rather than one copy per each individual instance for instance variables.
  • A static method can access a static variable
  • To make a constant in java, mark a variable as both static and final
  • A final static variable must be assigned a value either at the time it is declared, or in a static initializer

  • The naming convention for constants is to make the name all uppercase.
  • A final variable value cannot be changed once it has been assigned.
  • Assigning a value to a final instance variable must be either at the time it is declared, or in the constructor.
  • A final method cannot be overridden.
  • A final class cannot be extended (subclassed)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: