2
votes
package abc ;   
class Trying
{
Trying ()
{
    System.out.println("hello");
}
        }

public class trying {
public static void main(String[] args) {
    new Trying () ;
}

}

In this when I change the name of class from Trying to some other name it works , but here it says :

Error: Could not find or load main class abc.trying /Users/name/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

Why is this happening ? I didn't find such case in any of the questions already asked .

4
Which jdk and OS you are using? - Hitesh Ghuge
I have installed this : jdk-8u121-macosx-x64 (1).dmg - shikhar

4 Answers

1
votes

Java is case sensitive language, but there is no documentation that class name should be case sensitive or not.

In eclipse it will show you syntax error

Class file collision: A resource exists with a different case: '/sample/bin/abc/Trying.class'.

OR

If not shows error it will create class file of only one class either Trying or trying.

1) If class file of Trying class is generated then It will throw

Error: Main method not found in class abc.trying

Since there is no main method in class Trying, And at runtime it looking for main method to start.

2) If class file of trying class is generated then It will throw

Exception in thread "main" java.lang.NoClassDefFoundError:

here at runtime it looking for class Trying since it called in main of class trying. It fails to load coz its not compiled.

So we can conclude java not allow two class with sameName even different case

more details of case sensitive of class name is here

0
votes
class Trying
{
    Trying ()
    {
        System.out.println("hello");
    }
}

public class Try_Main {
    public static void main(String[] args) {
        new Trying () ;
    }
}

Please use two different class names other than same name with different cases. On compilation, the compilation will success and compile will create two class files with same name but different cases. But, the OS only allows a single file and it simply overwrites first one( which created firstly on compilation, then second) by the second one. On running, you will get a run time error, because one of the classes is missing. So, please use different names...

0
votes

Well, this class should be public and be sure of saving the file name as the class Name

-1
votes

You have specified Trying multiple times.

package abc;

public class Trying {

    public static void main(String[] args) {

        trying1();

    }

    public static void trying1() {

        System.out.println("Good?!");

    }

}