• Post Reply Bookmark Topic Watch Topic
  • New Topic

static variable  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1-static variable, Is shared between classes. Does it mean that is use less memory?

2-Another question:

in Interface each variable is final static. Final make find bug easy. But what is the advantage of using static here? becuase that variable is shared between classes must be static?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well...think about it. If i have one variable that is shared by every instance, I need to store it once.

If i have a variable in every instance, and I create 1000 instances, I need to store the value for that variable 1000 times.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static things can't see "instance" things, but instance things can see static things. That means every one of Fred's 1000 instances can "see" that static variable, all seeing the same value. If it were a variable, ie one can change it, then if one instance changes it, all other instances "see" that change.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer

1.Yes,As the variable is stored only once.

2.Static final makes the variable compile time constant,that means compiler is deciding about the state of variable
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you folks!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote: . . . 2.Static final makes the variable compile time constant,that means compiler is deciding about the state of variable
Not quite. This is the definition of compile-time constant. Some final references might not be compile-time constantsYou will get all hat to compile and execute if you create the appropriate Foo class and a main method.
You cannot change what a final reference points to, but if it is a mutable reference type, you may be able to change the state of that object. So it isn't quite as constant as you think.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Static things can't see "instance" things, but instance things can see static things. That means every one of Fred's 1000 instances can "see" that static variable, all seeing the same value. If it were a variable, ie one can change it, then if one instance changes it, all other instances "see" that change.


Thanks for that simple and to-the-point explanation! Gonna save it!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes your are right final reference are not compile time contant..

but if you see...

i am saying "static final" when used together with a variable will become a constant time constant.


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote: . . . i am saying "static final" when used together with a variable will become a constant time constant.
No, it won't. Have another look at the link I gave you about compile-time constants.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!