wood burning stoves*
The moose likes Beginning Java and the fly likes Why should the parameter be final? 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 » Java » Beginning Java
Bookmark "Why should the parameter be final?" Watch "Why should the parameter be final?" New topic
Author

Why should the parameter be final?

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Why should the parameter be declared final if we are going to use that in the initialization oa anonymous class? Why can't it be a normal variable just as any other method would have it.
And
Why can't we declare static variables and interfaces in an inner class
Thanks for any help


Thanks,
Shekar
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Why can't we declare static variables and interfaces in an inner class

Your question hints that you don't know what an inner class is. So maybe you are asking the wrong question. Tell me what you think an inner class is (this may answer your question above).


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Java in a Nutshell tells me there are 4 kinds of "inner" classes. Static member, Member, Local, and Anonymous.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
An inner class is a class which is declared inside a class. Isn't it? Normally a class can be declared inside an inner class. Now inside this inner class why can't we declare static members and interfaces. We can do that in a normal class but why not in an inner class. Hope the question is clear.
and what about the first question why should a parameter to method be declared final when you are using that inside an anonymous class?
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Originally posted by Chandra Bairi:
An inner class is a class which is declared inside a class. Isn't it? Normally a class can be declared inside an inner class. Now inside this inner class why can't we declare static members and interfaces. We can do that in a normal class but why not in an inner class. Hope the question is clear.

Because static meant that it belong to the Class, not belong to an Object instance. And the 'correct' way to access static member is by using the Class's name. When you have an anonymous inner class, well..it is anonymous, so you don't have a Class's name to access the static member.
Same goes to Interface.
As you know inner classifier(class/interface) is also referred by their Outer.Inner name. So with regular class you can have innter class/interface which you can access through it's name, but with anonymous inner class you can't. Especially when considering the purpose of Interface is to provide a contractual obligation for other class to implement. So even if you have an interface inside anonymous inner class, you can't implement the interface since you can't refer to it.
[ October 28, 2003: Message edited by: Wirianto Djunaidi ]
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
please consider the following piece of code:
class One {
class Two {
static int i;
interface TwoInt {
}
}
}
"Two" is an inner class but it does not allow static and interface declarations why here we can access the class "Two" as follows :
One.Two I suppose it should be legal here. why does it not allow ?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Wirianto answered your question. You need to understand that post.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
i will explain the concept since the static member in an inner class cannot be called directly by referring Outerclass.innerclass.staticmember i suppose the inner non-static classes cannot allow static declarations but why should they not allow the interfaces is it the same reason?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

An inner class can only be used by the outer class.
An interface can't be instantiated.
So how would you ever be able to use an interface defined as an inner class?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why should the parameter be final?
 
Similar Threads
Use of Final Class
final class
doubt in constructor
Problem while trapping the navigation keys
Object class doubt