• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to run main() method in nested static class?

 
Ranch Hand
Posts: 80
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to run main() method from nested static class?

I compile and run both. But they all seem to call the main() method from Outer class only. It only prints "Print from Outer class".

See attached screenshot.

javac Outer.java
java Outer
java Outer$StaticNestedClass

staticnestedclass.jpg
[Thumbnail for staticnestedclass.jpg]
 
Ranch Hand
Posts: 127
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from me think this isn't possible at all there's at least the issue neither the outer nor the inner class are public. So, I wonder how this works at all. If I'm correct for a class to be able to be the main one it has to be public - but this may had changed in the past.
As for how to call it: If this works at all, you have to call the classname correctly:
java Outer.StaticNestedClass
The $ is just the delimiter for filenames, but in terms of how Java addresses it by classname is still with the single dot .
 
Marshal
Posts: 70224
282
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid screenshots. They are difficult to read.

You can find out the name of the class as seen by the command line with the ls instruction (Unix/Linux/Mac):

campbell@campbellsComputer:~$ ls *.class
. . . Outer.class . . . 'Outer$Inner.class' . . .

The name of the class is regarded as 'Outer$Inner.class'.

java 'Outer$Inner'
Hello, Inner World.

You need the quotes. And it doesn't seem to matter whether either class is public.

java Outer.Inner
Error: Could not find or load main class Outer.Inner
Caused by: java.lang.ClassNotFoundException: Outer.Inner

java Outer$Inner
Error: Main method not found in class Outer, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

 
Campbell Ritchie
Marshal
Posts: 70224
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are lucky I didn't move this discussion to “MD
 
Saloon Keeper
Posts: 12253
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Winter wrote:If I'm correct for a class to be able to be the main one it has to be public - but this may had changed in the past.


I don't believe it was ever this way. For as long as I can remember, you can always use a package private class as the main class. You just can't use a private nested class.

Campbell Ritchie wrote:java 'Outer$Inner'


It surprises me that this is the correct command. The Oracle docs on the 'java' command line tool say that it expects the main class to be specified using a fully qualified class name. Outer$Inner is not the fully qualified name, but rather the binary name. This implies that either the 'java' tool uses the wrong name or, more likely, the Oracle docs mistakenly use "fully qualified class name" when they meant to use "binary name".
 
Campbell Ritchie
Marshal
Posts: 70224
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the fully qualified name, too, because I didin't give it a package name. Let's try. Back in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 70224
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

campbell@campbellsComputer:~$ javac main/Outer.java
campbell@campbellsComputer:~$ ls main/*
'main/Outer$Inner.class'   main/Outer.class   main/Outer.java
campbell@campbellsComputer:~$ java 'main/Outer$Inner'
Hello, private Inner World
campbell@campbellsComputer:~$ java main.Outer
Hello, Big Wide packaged World.
campbell@campbellsComputer:~$ java 'main/Outer'
Hello, Big Wide packaged World.
campbell@campbellsComputer:~$ java main/Outer
Hello, Big Wide packaged World.
campbell@campbellsComputer:~$ java main/Outer.Inner
Error: Could not find or load main class main.Outer.Inner
Caused by: java.lang.ClassNotFoundException: main.Outer.Inner
campbell@campbellsComputer:~$ java main/Outer$Inner
Hello, Big Wide packaged World.
campbell@campbellsComputer:~$ java main.Outer.Inner
Error: Could not find or load main class main.Outer.Inner
Caused by: java.lang.ClassNotFoundException: main.Outer.Inner

Good grief!

Ubuntu Focal (20-04) and JDK 14.0.1.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The name of the class is regarded as 'Outer$Inner.class'.

java 'Outer$Inner'
Hello, Inner World.

You need the quotes.


The name is the class as you'd need it for reflection is Outer$Inner. That may not be the fully qualified name as you'd use in code, but reflection simply needs the $.

The quotes are necessary, at least in PowerShell and most Unix shells, because the $ has special meaning there. You can even see that - in the screenshot, the $ and everything after it is regarded as a variable. As no such variable exists, its value is empty. What remains is java Outer.
 
henry leu
Ranch Hand
Posts: 80
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!

Thanks for all the answers.

I think I found the problem.

And here is my solution.

I was on Windows Powershell. So I need to use the single quote on the class name.
javac Outer.java
java 'Outer'
java 'Outer$StaticNestedClass'

And in Windows Command Prompt, I can just run it without the single quote.
javac Outer.java
java Outer
java Outer$StaticNestedClass

 
Stephan van Hulst
Saloon Keeper
Posts: 12253
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is the fully qualified name, too, because I didin't give it a package name. Let's try. Back in a few minutes.


The fully qualified name in your latest example would be main.Outer.Inner. The binary name is main.Outer$Inner. Clearly, 'java' requires the binary name, but their documentation states otherwise. One of the two is mistaken.
 
Campbell Ritchie
Marshal
Posts: 70224
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: writing main/Outer or main/Outer$Inner with or without quotes provides something other than the fully qualified name. I got a no class found exception yesterday when I used the real fully qualified name.
 
You showed up just in time for the waffles! And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic