• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion Here.. static contexts.

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does one need to declare a variable as both static final? for eg interface variables are implicitly final..
Anyone with an example...this isn't striking me .

What is the purpose of assigning a constructor as a static ..can it be assigned at all.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar:
Why does one need to declare a variable as both static final? for eg interface variables are implicitly final..
Anyone with an example...this isn't striking me .


okay. Here is the deal,
1. static is used to declare 'class' level variable. Now, it could be the case that we want to allow changing the value for this variable.
2. final is used to say 'none can change the variable's value after it gets initialized once'
I did googling on this and I would consider this
link for our reference in the explanation.
If we apply the above two point of knowledge then we could see-
1. some variable may just have to be static only. e.g. the variable shapesCount is just static variable in ShapesCounter class. Why? Because we wanted that variable to get updated by each of the Shapes when they get created, right?
2. some variable may need to be final "in addition to being static". e.g. maxShapeCount variable in ShapesCounter is "final". Why? Because we want to restrict maximum number of Shapes objects in our application to 100.
Does this make sense?



What is the purpose of assigning a constructor as a static ..can it be assigned at all.


Constrcutor can't be static. May be somebody can provide you a link of JLS (Java Language Specification)'s particular section about this....
Regards
Maulin
 
reply
    Bookmark Topic Watch Topic
  • New Topic