• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

static variable vs final variable!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
May I please know the difference between the static and final variables.I know that both are constants.can anybody explain in detail pl!Thanks
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar,
i dont think static variables are constants.coz constants are those whose values cannot be changed once they are assigned a value.
But, ofcourse final variables are constants.once final variables r intialized they can never be changed.
the diff is tht each instance of class has its own copy of final variables whereas static variables are associated with classes rather than with instances of classes.i.e. there exists only one copy of static variables and all the instances share the static variables.so,if u change the vlaue of the static varible of the first obj, then the value of the second obj will also be changed.
eg:
class Test{
final int i = 9;
static int j = 3;
public static void main(String args[]){
Test t = new Test();
Test t1 = new Test();
t.i++; //illegal - u cannot change final value
t.j++;
System.out.println(t.j); // line 1
System.out.println(t1.j); // line 2
}
}
u can observe tht o/p of line 1 will be same as line 2 since j is declared as static.
Hope u got it.
vineela
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!I got it.
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A variable can be static, final, or both.
Try this code below.
reply
    Bookmark Topic Watch Topic
  • New Topic