• 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

Inner Interfaces

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read a line in Khalid Mughal and it states that

"Nested Interfaces are always static ".

I executed the following and it compiled okay.


Please do confirm the above statement, is it wrong or wrongly interpreted by me ???

Thanks !!!

2) Please do consider statement

" No other static memebers, except final static fields, can be declared within a non-static member class"

This statement is given Correct. But i tried the follwing code snippet and it failed to compile.



So, please do confirm this statement too.... In my consideration , the above statement is false.

Thanks !!!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil Rally wrote:I have read a line in Khalid Mughal and it states that

"Nested Interfaces are always static ".

I executed the following and it compiled okay.


Please do confirm the above statement, is it wrong or wrongly interpreted by me ???

Thanks !!!

2) Please do consider statement

" No other static memebers, except final static fields, can be declared within a non-static member class"

This statement is given Correct. But i tried the follwing code snippet and it failed to compile.



So, please do confirm this statement too.... In my consideration , the above statement is false.

Thanks !!!



Your first question isn't clear..

For the second, It's the problem related with the initialization of final fields.... Not related to the inner class...

Final variable should be initialized when the declared or in every Constructor of the class

Hope this helps~!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And since the inner class can have static final fields, rather than final field, they(static final fields) are compile time time constants. So you can't initialize them in Constructors rather than on the declaration time..

 
Master Rancher
Posts: 5005
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first question, nested interfaces are always implicitly static. Meaning they're automatically static even if you don't declare them as static.

For the second question, I don't understand your confusion. The statement was made that a non-static member class cannot have any static fields, unless they are either inherited or they are compile-time constants (which is what Abimaran describes). You tried to make a non-static member class with a static field. It didn't compile. The error message you get is "inner classes cannot have static declarations". So what's the problem? Mughal & Rasmussen said you can't do this, and the compiler also told you that you can't do this. M&R are correct.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@both Thanks i got it !!!
Cheers!!!
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't studied inner classes yet, but what is the design logic for the Java programming language NOT to allow inner classes to have static declarations. Is there only ONE instance of an inner class, hence there is no need to have any static variables? Please clarify.
 
We're all out of roofs. But we still have tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic