Asked 7 years ago
4 Jan 2017
Views 1203

posted

diffrence between Static and final keyword in java

what is difference between keyword "Static" and "final" keyword in java , what are the use of it so we can justify and utilize object oriented concept , if good example will great help
Nilesh

Nilesh
answered Nov 30 '-1 00:00

we cant say its opposite party like final VS static . both are keyword and have differ purpose for making it and one can use it as per need . so understand its scope and property , one can use it very well.

static keyword
Static variable is static for instance of particular class . it means when you declare a variable static it remain common for class and its all instance , share same memory , so we can say its static , remain same for all instance

static variable can be only accessed through its class name

final keyword
final variable means its contain value is final . its not changeable in other word it is constant
final class can not be sub classed . its final version of class no inheritance supported for it
ravi

ravi
answered Nov 30 '-1 00:00

one can define static and final variable at a time.


public static final int DEFAULT_SIZE = -1


above initialization DEFAULT_SIZE is public : means available to all , static : means share to all object for class , final : means its not changeable

so it is constant which shared with all object of that class.

static save memory by creating one reference for all object , and by final , no one can change even if it public

one cant change final value ,

javax.swing.GroupLayout.Alignment.LEADING = 2;

by trying to change final value of java swing GroupLayout Alignment LEADING , got error like this


cannot assign a value to final variable LEADING

incompatible types: int cannot be converted to Alignment

Post Answer