0

In Java, I can a run/execute a non public (i.e. 'default' access) java class from the command line even though it is in a package. Why should this be possible? are we not thereby using a default class outside its package which is not supposed to be correct?

KXL
  • 345
  • 3
  • 6
  • Is the method `public`? – bradimus Jul 03 '17 at 11:51
  • What do you mean you can "execute" a class? Does your class implement `Runnable` or something? – M. Prokhorov Jul 03 '17 at 11:51
  • can we see the package-class skeleton ?? – ΦXocę 웃 Пepeúpa ツ Jul 03 '17 at 11:51
  • From my part, is is not clear. You can only execute a `public static void main(String[])` method from command line (as the [JLS said](https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html)). The visibility of the class don't matter ( but I need to confirm that last part ) – AxelH Jul 03 '17 at 11:51
  • Please give a [mcve]. Like the others I am not sure what you are asking for. – GhostCat Jul 03 '17 at 11:51
  • Presuming that you're asking why you can run the `main(String[])` method in a package-private class—why should it be prevented? What advantage would that give? – khelwood Jul 03 '17 at 12:01
  • private classes are being used outside their context all the time, e.g. nested class `private java.util.Arrays.ArrayList`, returned by `Arrays.asList(...)`, (not to be confused with `public java.util.ArrayList`) – user85421 Jul 03 '17 at 12:12
  • @KXL: I think this is a good question (and I hope to get some further inputs/ideas to my answer). But it looks like it does get downvotes as it's not very clearly formulated. Could you please try to improve your question? – sruetti Jul 03 '17 at 12:45

3 Answers3

1

I think you're describing something like the following (See Is a class private or public by default in Java and C++? about the available visibility modifiers on a java class):

class PkgPrivateClass {

  public void doIt() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    PkgPrivateClass pkgPrivateClass = new PkgPrivateClass();
    pkgPrivateClass.doIt();
  }
}

This compiles and prints Hello World!.

Now for the question what is accessible and why:

  • The main method doesn't and can't do anything other than every static method.
  • The main method is only visible inside the same package (as the class is package private). Starting a java program - i.e. calling the main method - is hardly comparable to an ordinary method call within a java program.

While package private classes are anyway not used that often (see Pros and cons of package private classes in Java?), I see two arguments why it makes sense to allow calling package private classes from the command line:

  • You explicitly added the public static void main(String[] args) method. If you don't want this class to be called, you don't add this method.

  • When starting the JVM, you usually give the fully qualified class name. One could argue that this makes the command line call executed from this package - or asked the other way around: From what package is a main method called?

sruetti
  • 532
  • 2
  • 7
0

The access modifier default has another name which is called as package specific. So if you are in the same package for a default class you can use command line execution. Outside the package you can not execute default class directly.

rackdevelop247
  • 86
  • 1
  • 10
0

main() invoked via reflection, that ignores class visibility. You can write own executor instead of java.exe, that will check class visibility too, or do NOT check visibility of main() for example.

rustot
  • 331
  • 1
  • 11