Why can't I have a java class name and interface name that only differ in letter case?

java

Solution

Technically, you can do this on some platforms.

- It will work on Linux / UNIX

- It will probably work on Mac OSX, though you may need to tweak things to turn of "user-friendly" case-insensitivity.

However, it is a bad idea to make your code-base dependent on the platform's ability to do case-sensitive pathname lookup. And it is also a bad idea to ignore the Java Style Conventions which clearly state that:

- you should not define identifiers of the same kind that differ only in case,

- you should always start a class name with an uppercase letter, and

- all uppercase is reserved for constants.

The problem is that the standard Java mechanism for finding the file in which a class lives relies on being able to map the class name to a file name, on the assumption that filenames are case sensitive. If you follow the Java style guidelines, the mechanism works on all platforms. If you (wilfully) don't, you are going to be in for a world of pain ... and you won't get much sympathy.

(But if you managed to put your compiled classes into a JAR file with the correct casing for the class names, that should work even on Windows. Not sure of a good way to do that though ... if you are building on Windows.)

So, to answer your question:

why can't i use similar word as java class name and interface name which just case differs?

Because you are using Windows, and because you are ignoring Java style rules.

JAVA is not case sensitive, is it?

Yes it is.

I should also point out that `Show()` is a style violation, and so are `changespeed(...)`, `changeoil(...)` and `_speed`, and `_oil`.

If you are writing code that only you will ever read, then you can (mostly) get away with ignoring style. But if you write code that other people will / might have to read, then you are liable to get a lot of criticism.

Problem

I create a class named `CAR` and I also created an interface named `car`. They both in a same source file. I make the `CAR` class implements the `car` interface and the IDE shows nothing wrong. But when I run this program, it gives an error that is ``` Exception in thread "main" java.lang.NoClassDefFoundError: test/car (wrong name: test/CAR)" ``` Why is that ? JAVA is not case sensitive, is it? Here's the code: ``` package test; interface car { void changespeed(int x); void changeoil(int x); } class CAR implements car { private int speed; private int oil; public CAR(int _speed,int _oil) { speed = _speed; oil = _oil; } public void changespeed(int x) { speed = x; } public void changeoil(int x) { oil = x; } public void Show() { System.out.printf(speed + " " + oil); } } public class test { public static void main (String[] args) { CAR a = new CAR(100,200); a.changespeed(200); a.changeoil(200); a.Show(); } } ```

Original source