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

can u run main method in an inner class directly?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c the code

compile the Test class javac Test.java
Test.class and Test$I.class are created..
I tried to run the inner class by giving java Test$I.java..got error noClassDefFoundError..why cant u run an innerclass like this??can because only toplevel classes be run using java classname.java command?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it'll work -- you just need to call it correctly.

So if you were going to compile/run the class "Blah.java" you'd do this:
> javac Blah.java
// this creates Blah.class
> java Blah

In the same token, if you wanted to compile and run the inner class within "Test.java" you'd do this:
>javac Test.java
// this creates Test.class and Test$I.class
>java Test$I
// and voila! it prints 3
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic