File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static " Watch "static " New topic
Author

static

HansLi
Greenhorn

Joined: Oct 06, 2000
Posts: 1
I read a book that say non-static inner class can have static member.I think it is wrong.Because inner instance must associate with outer class instance.Why the book say that.Who know reason?Thank you.
Harry Singh
Ranch Hand

Joined: Sep 22, 2000
Posts: 53
Hans,
You sound like you could write a better book on inner classes than the one you are reading. You are quite right and the book seems wrong. Only top level nested classes ( the only ones which are allowed to use a static keyword as part of their declaration ) can declare static memebers. What book is that ? You can tell all of us so we can avoid it ...
Thanks
Lahcen Mannou
Greenhorn

Joined: Oct 09, 2000
Posts: 7
Hi,
Please can someone explain why a static variable is not permitted in a non-static inner class ?
Thanks in advance
Mukesh Rathod
Ranch Hand

Joined: Oct 11, 2000
Posts: 32
Hi
I am new member.
Well, the book seems to be correct!! Non- static inner class can have static member variable, provided they r final.
Try the following code:
class TestInnerClass{
class Inner{
public static final int i = 10;
public void aMethod(){
System.out.println("Static i = " + i);
}
}// end of inner class
public static void main(String args []){
new TestInnerClass().new Inner().aMethod();
}
}// End of outer class

Mukesh

Originally posted by HansLi:
I read a book that say non-static inner class can have static member.I think it is wrong.Because inner instance must associate with outer class instance.Why the book say that.Who know reason?Thank you.

Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Correction:
Only package-level class, top-level nested class(static), and interface can define static memebers in their class.
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static