-1
public class battle10 extends battle101
{
    public static void main(String a[])
    {
        battle10 obj=new battle10();
    }
}

class battle101
{
    class battle10
    {
        public void life()
        {
            System.out.println("I love Mango");
        }
    }
}

In the above code, we use public class battle10 and class battle10 inside class battle101, Is public class battle10 conflicts with class battle10 or not? If not then why? Please give me your answers, I'm new to java

  • 4
    When you try to run your code, what happens? Why would you go to all the trouble of writing that code and then stop just short of running it? – MarsAtomic Mar 23 '15 at 22:55
  • check out this. http://stackoverflow.com/questions/8994147/what-are-the-implications-of-having-duplicate-classes-in-java-jar. Your program should run, but the classloader will determine which gets preference. I would take a guess and say the inner battle10 (of battle101) would get loaded first. – jcd Mar 23 '15 at 23:10
  • 2
    Yes, you can, but it's a bad idea for obvious reasons. – user253751 Mar 23 '15 at 23:16
  • 1
    @jcd no, this is nothing to do with classloaders. – user253751 Mar 23 '15 at 23:17
  • why classloader gives more preference to inner **class battle10** than **public class battle10** when compile. – RajanArora Mar 25 '15 at 00:12
  • @RajanArora It doesn't. I was wrong. – jcd Mar 30 '15 at 16:18

3 Answers3

0

To specifically answer your question.

Java packaging is pretty simple. The only way to get a namespace collision is to have two classes in the same package with the same name.

package com.game.battles

public class Battle10{
}

package com.game.battles.behavior

public class Battle10{
}

package com.game.battles

public class Battle101 extends com.game.battles.Battle10{
    public static class Battle10{ 
    }
}

the battle 10 within the battle 101 has the namespace:

com.game.battles.Battle101.Battle10

the battle10 outside

com.game.battles.Battle10

if you refer to both classes in the same block of code, declaring the fully qualified path will let you grab the correct one regardless of name.

com.game.battles.Battle10 battle10 = new com.game.battles.Battle10()

com.games.battle.Battle101.Battle10 innerBattle = new com.games.battles.Battle101.Battle10()

In our example we have 3 battle10 classes. one of which is inner, two of which in different packages.

None of these have namespace collisions... but need to be fully qualified on use.

I recommend maybe restructuring your code to avoid this namespace mess.

as a side note: this is cumbersome, but possible. It is highly discouraged to make your code look like this.

AnthonyJClink
  • 1,008
  • 2
  • 11
  • 32
-1

Yes, at different levels you can use same class name but that is not advisable. It is not a good programming practice to do so because I think it makes your code less readable and also hard to go through while debugging. But you cannot name the inner class same as the outer class.

In your case, the conflict doesn't occur because the compiler treats them differently. I think this is because the Java Classloader dynamically loads Java classes into JVM. So your inner class is not loaded by the time outer class is loaded.

I have read another question that has some good info about this. Refer Does the Java ClassLoader load inner classes?

Community
  • 1
  • 1
Prudhvi
  • 2,276
  • 7
  • 34
  • 54
-1

You have to re-write the syntax as new battle101().new battle10(); to make it compile.

When you instantiate new battle10() , the compiler is confused which one to instantiate. This is because the "inner battle10()" is enclosed inside "public battle10" by extending "battle101".

Consider making inner class battle10 as private as,

class battle101 {
    private class battle10 {
        public void life() {
            System.out.println("I love Mango");
        }
    }
}

Now the compiler can distinguish the inner battle10 with public battle10 and your code compiles.

Hope this brings some clarity ... :-)

tbodt
  • 16,609
  • 6
  • 58
  • 83
Sasi
  • 9
  • 2