• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Why should the parameter be final?

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic