• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static variable

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you folks!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic