Sunday 9 December 2012

Difference between final and static variables

Leave a Comment
Whenever you declare a variable as static,then the static variable will have a single copy that is shared by all the objects.

And we can access static variable directly by using class name
Example : ClassName.VariableName

so any modifications to the static variable will modify it's value in all objects.

Final Variables: The value of a final variable cannot change after it has been initialized.final keyword is for telling that this is constant weather it is variable or method we cannot change final variable value and cannot override final method(CANNOT CHANGE ITS DEFINITION).

PS:we cannot override static methods but we actually hide the super implementation when we try to override them.

try this:

class Foo 
{
 public static void method()
 {
   System.out.println("in Foo");
 }
}

class Bar extends Foo 
{
  public static void method() 
  {
   System.out.println("in Bar");
  }
}

0 comments:

Post a Comment