A class with no modifier (default) cannot access a subclass declared as public? Java

huangapple go评论79阅读模式
英文:

A class with no modifier (default) cannot access a subclass declared as public? Java

问题

I was just doing practice on Hackerrank since I'm still pretty new to Java (I'm only experienced with C and C++, with minimal Python/Matlab/C#). Basically we only had to write the "Checker class" below from scratch. However, I noticed that when I add public to the Checker class it results in runtime error. Does anyone know why? I couldn't find any answers on this online.

Also, yes, I know access modifiers restrictions on how much they can have access to the scope of classes, but it does not make sense to me on how a default class cannot access a public class's method. I'm assuming it is perhaps I'm implementing a parent class that's causing the problem? Here is the RE message I receive on Hackerrank:

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

If interested, link to the practice problem for reference: https://www.hackerrank.com/challenges/java-comparator/problem

import java.util.*;

// Write your Checker class here
class Checker implements Comparator<Player> {  // If I add "public" in front I get RE
    @Override
    public int compare(Player A, Player B) {
        if (A.score == B.score)
            return A.name.compareTo(B.name);
        else
            return B.score - A.score;
        // return A.compareTo(B);
    }
}

class Player {
    String name;
    int score;

    Player(String name, int score) {
        this.name = name;
        this.score = score;
    }
}

class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();

        Player[] player = new Player[n];
        Checker checker = new Checker();

        for (int i = 0; i < n; i++) {
            player[i] = new Player(scan.next(), scan.nextInt());
        }
        scan.close();

        Arrays.sort(player, checker);
        for (int i = 0; i < player.length; i++) {
            System.out.printf("%s %s\n", player[i].name, player[i].score);
        }
    }
}
英文:

I was just doing practice on Hackerrank since I'm still pretty new to Java (I'm only experienced with C and C++, with minimal Python/Matlab/C#). Basically we only had to write the "Checker class" below from scratch. However, I noticed that when I add public to the Checker class it results in runtime error. Does anyone know why? I couldn't find any answers on this online.

Also, yes, I know access modifiers restrictions on how much they can have access to the scope of classes, but it does not make sense to me on how a default class cannot access a public class's method. I'm assuming it is perhaps I'm implementing a parent class that's causing the problem? Here is the RE message I receive on Hackerrank:

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

If interested, link to the practice problem for reference: https://www.hackerrank.com/challenges/java-comparator/problem

import java.util.*;
// Write your Checker class here
class Checker implements Comparator&lt;Player&gt;{  //If I add &quot;public&quot; in front I get RE
@Override
public int compare(Player A, Player B){
if(A.score == B.score)
return A.name.compareTo(B.name);
else
return B.score - A.score;
// return A.compareTo(B);
}
}
class Player{
String name;
int score;
Player(String name, int score){
this.name = name;
this.score = score;
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Player[] player = new Player[n];
Checker checker = new Checker();
for(int i = 0; i &lt; n; i++){
player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
Arrays.sort(player, checker);
for(int i = 0; i &lt; player.length; i++){
System.out.printf(&quot;%s %s\n&quot;, player[i].name, player[i].score);
}
}
}

答案1

得分: 0

Interesting question but I've never used this in my whole career.

> 如果源文件中没有公共类,那么主方法可以位于任何类中,并且我们可以给源文件任意名称。

来源:https://dzone.com/articles/why-single-java-source-file-can-not-have-more-than

由于@Andy Turner的评论,我进一步测试了一下,确实可以在其他类(而不是公共类)中拥有主方法。这完全取决于您如何调用它:

package sample;

class Problem  {
    public static void main(String[] args) {
        System.out.println("Problem的主方法");
    }
}

public class Solution {
    public static void main(String[] args) {
        System.out.println("Solution的主方法");
    }
}

源文件的名称必须是Solution.java,因为这是公共类,但您可以调用两个主方法:

> java sample.Solution
Solution的主方法

> java sample.Problem
Problem的主方法

当您从Solution中删除主方法时,仍然可以调用sample.Problem。

英文:

Interesting question but I've never used this in my whole career.

> If there is no public class in the source file then main method can
> lie in any class and we can give any name to the source file.

Source: https://dzone.com/articles/why-single-java-source-file-can-not-have-more-than

Because of the remark of @Andy Turner, I tested it a little bit further and indeed you can have a main method in other classes (other than the public one). It all depends on how you call it:

package sample;
class Problem  {
public static void main(String[] args) {
System.out.println(&quot;main of Problem&quot;);
}
}
public class Solution {
public static void main(String[] args) {
System.out.println(&quot;main of Solution&quot;);
}
}

The source file name must be Solution.java as this is the public class but you can call both main methods:

  &gt; java sample.Solution  
main of Solution
&gt; java sample.Problem
main of Problem

You can still call sample.Problem when you remove the main method from Solution.

答案2

得分: 0

While it's against "customs," it is possible to do what you tried. However the main() method is present in the Solution class, so that's the one you have to run.

From command line you can do that easily:

javac Checker.java
java Solution

as Solution.class will be generated properly.

However if you use an IDE, which you are not very familiar with, you may encounter difficulties when trying to tell them to run a different .class file from the .java they have just compiled. In short: name the file as Solution.java.

英文:

While it's against "customs", it is possible to do what you tried. However the main() method is present in the Solution class, so that's the one you have to run.

From command line you can do that easily:

javac Checker.java
java Solution

as Solution.class will be generated properly.

However if you use an IDE, which you are not very familiar with, you may encounter difficulties when trying to tell them to run a different .class file from the .java they have just compiled. In short: name the file as Solution.java.

huangapple
  • 本文由 发表于 2020年8月11日 16:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63353970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定