This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

no static method in inner class - why ?  RSS feed

 
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


after compilation there will be two class files : Outer.class & Outer$Inner.class

When we will run this inner class with java Outer$Inner command this will give an error NoSuchMethodExist . Because it doesn't have a main method .And we can't have a main method in inner class because this method has static in its signature .

Now my question is why inner class can't have static methods ???


thanks a lot .
[ January 13, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must never use the generated Outer$Inner.class file. In the case of your class and it's inner class you must have an instance of the outer class to be able to create an instance of its inner class.

"why inner class can't have static methods ???"

Because the JLS 8.1.2 says they can't.

And the Java Tutorial says
"Also, because an inner class is associated with an instance, it cannot define any static members itself."
[ January 13, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this:



The outer class can be an instance class and the nested class could be static. This way you can implement static void main in the nested class and it will run.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But remember, a static nested class is not an inner class.
 
Those are the largest trousers in the world! Especially when next to this ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!