user2588353
user2588353

Reputation: 1

have a same class name issue in two different java files

i have two java files named OverloadCons.java and OverloadCons2.java both containing two classes each. one class for the main method named OverloadCons/OverloadCons2 in both files and the other class is named Box in bothe the files i compiled and it worked perfectly but how can it work when both files contain same class named Box(not conatining the main method)

Upvotes: 0

Views: 130

Answers (3)

vishwampandya
vishwampandya

Reputation: 1187

When you have compiled both the files it must be looking like this(ie, only one box.class class file) folder containing your files

enter image description here

Now the Box.class file here contains the code for the most recent file(Overloadingcons.java or Overloadingcons2.java) that has been compiled.

for example - if you compiled Overloadingcons2.java most recent and try to run Overloadingcons.java then class Box which was written in Overloadingcons2.java will get executed.

hence now if you have this kind of situation then you must compile and execute the file together , otherwise it won't work properly every time.

Upvotes: 0

StephenTG
StephenTG

Reputation: 2657

This works for essentially the same reason that you can have multiple classes each have the same function (eg toString() or the like)

Since they have different outer classes, there's no overlap

Upvotes: 0

m0skit0
m0skit0

Reputation: 25874

Because one is OverloadCons.Box and the other is OverloadCons2.Box.

Upvotes: 4

Related Questions