Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why should the parameter be final?

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java in a Nutshell tells me there are 4 kinds of "inner" classes. Static member, Member, Local, and Anonymous.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wirianto answered your question. You need to understand that post.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic