• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restriction on Constants

 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I think final modifier is must for constants,because the values of constants can't be changed.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic