Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Inner class and static members

 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi know that there's been some discussion on weather static members allowed to be in inner classes. But i still think that they are not allowed. Let's say we get a question like this is static member allowed in a class? The answer i think is no. If you place a final in front of it, it will work but now it's a constant and compiler treats this variable differently. Also i havent seen anywhere in any of the books saying it's true. So do we answer that they are not allowed in inner class or do we say they do ? Logical answer would be NO to me and my classmates and co-workers.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did saw a question in JQ+ like that and the answer they say is yes. static members are allowed in innerclasses. also in KAM u can get similar question. What i feel is once a member is declared static, it doesn't matter whether it is final or not.

------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Val:
You can definitely declare static members in a static top-level nested class. But you can't have any static members in other three kinds of nested classes(non-static inner classes, local classes and anonymous classes). Then your question come down to: Can we take the above four kinds of nested classes as inner classes? Khalid only believe the last three can be counted as inner classes. But R&H include static top-level class as static inner class. It seems that R&H version is more widely accepted(like, Java in a Nutshell). So you should say Yes to the question. But anyway, as long as you understand the why the other three inner classes can't have static member( because they are all associated with an instance of the enclosing class) , it doesn't matter what is the correct answer to your question.
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, when I try to stick a non-final static variable into an non-static inner class, the compiler complains something like:
can't declare static member in inner class.(I'm sorry that I can't give the precise English, because my Chinese operating system automatically tranlates for me, which annoys me a lot, but I can't dump the operating system just because of that)
Don't count on the compiler complainings, as my other posting shows: http://www.javaranch.com/ubb/Forum24/HTML/008067.html
[This message has been edited by Tom Tang (edited February 12, 2001).]
 
Everybody's invited. Except this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic