• 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

Restriction on Constants

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a small question that came into my mind and i thought why shouldn't i ask that.so here it is.

Can Constants be declared in any other way than public static final. Can we remove any of the specifier while declaring them or all the three are must?


whats the error in this code?



Waiting for your reply.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sure... Why not? A constant is merely a variable that holds a value that doesn't change.

And there are times that (1) you don't want the variable accessed at a public level (hence, remove the public), (2) you want more than one copy of the constant, such as one copy per instance (hence, remove the static), and (3) the constant may actually change, and isn't really completely constant (hence, remove the final).

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Sethi wrote:
whats the error in this code?



Didn't someone already answer this question in another one of your topics?

Basically, local variables are local to the method, and have a scope that is during the lifetime of the method.... and the "public" and "static" modifiers don't make sense in this regard, hence, illegal.

Henry
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But this is not an instance method it is static method( main),so its scope will be as long as the class is loaded.And it can also be accessed by class name .I have checked that.so whats the problem.See this code it runs fine.


 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i got my answer ,tell me if i am right.Since the constant z is defined in a static method ,that means we can not access it directly with class name however we can access the static method.And the static methods are meant to be accessed by the class name .That's why there was an error in first code.


So does that mean we can not define anything static within a static method/instance method and static variables and methods are meant to be declared at class level only.Is my interpretation correct?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Sethi wrote:But this is not an instance method it is static method( main),so its scope will be as long as the class is loaded.



Variables defined within a method are local variables, and as such, has a scope of the method call itself. And this is true regardless of whether it is an instance or static method.


Harshit Sethi wrote:And it can also be accessed by class name .I have checked that.so whats the problem.See this code it runs fine.




You don't have any local variables declared in this new example.

Henry
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think final modifier is must for constants,because the values of constants can't be changed.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Sethi wrote:
So does that mean we can not define anything static within a static method/instance method and static variables and methods are meant to be declared at class level only.Is my interpretation correct?



It's not clear, what you are trying to say?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Sethi wrote:I think final modifier is must for constants,because the values of constants can't be changed.


Obvious!

Henry Wong wrote:
...the constant may actually change, and isn't really completely constant (hence, remove the final).



Don't you notice?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Sethi wrote:
So does that mean we can not define anything static within a static method/instance method and static variables and methods are meant to be declared at class level only.Is my interpretation correct?


exactly! you got the point! shortly, you cant declare anything inside a method as static
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thing ... only final is allowed to be applied on variables inside a method definition
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the constant may actually change, and isn't really completely constant (hence, remove the final).



But Constants are always constants,their value does not change .You are mixing constants and variables together.

If what you are saying that constant changes if final is removed then it does not remain a constant it becomes a variable and i am talking about Constants in this post so ,that's not so obvious my friend.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...the constant may actually change, and isn't really completely constant (hence, remove the final).


But Constants are always constants,their value does not change



I might be completly off here, but what I think Henry was trying to explain is, if the costant is a reference variable and the object is mutable in some situations you can't guarantee that the 'content's of referenced object will stay the same. (note to self, write shortes sentences!) An example:



In this case MY_CLASS is in fact a constant. It's reference cannot be changed. Putting something like this in the main method won't compile.


But we can change MY_CLASS 'inner values' in souch case it might make more sense to remove the final modifier, since it hardly makes a difference.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Sethi wrote:
But Constants are always constants,their value does not change .You are mixing constants and variables together.

If what you are saying that constant changes if final is removed then it does not remain a constant it becomes a variable and i am talking about Constants in this post so ,that's not so obvious my friend.



I am using the term "constant" loosely here. And since this topic is talking about how to implement constants for programs, without providing a hard definition for it, I am allowed to do that.

Constants are values that aren't supposed to change -- but they do. Have you ever worked with a user on setting up configuration constants? Do they ever come back to have it changed? Or heck... do they even ask for a way to change it themselves? And yes, technically, it isn't a constant anymore, but after a few decades of this, I learned not to take the definition too serious, since users don't.

Henry
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic