This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Restriction on Constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Restriction on Constants" Watch "Restriction on Constants" New topic
Author

Restriction on Constants

Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
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
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40

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

Joined: Jul 16, 2010
Posts: 75
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

Joined: Jul 16, 2010
Posts: 75
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
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40

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

Joined: Jul 16, 2010
Posts: 75
I think final modifier is must for constants,because the values of constants can't be changed.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 08, 2010
Posts: 384

one more thing ... only final is allowed to be applied on variables inside a method definition


http://plainoldjavaobject.blogspot.in
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
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

Joined: Sep 16, 2008
Posts: 241
...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.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40

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
 
 
subject: Restriction on Constants
 
Similar Threads
Switch statement and constant variables
Mock exam question
Which one is better
Super and sub class
Enum vs Constant File