Kishore rajan
Kishore rajan

Reputation: 3

what happens when we have two classes with same names in java?

I have the following files in the same directory.

here is my code

boys.java

import java.io.*;

public class boys
{
    String name;
    int age;

    boys()
    {
        this.name="empty";
        this.age=0;
    }

    void display()
    {
        System.out.println("Name =" + name + "\nAge ="+ age );
    }

}   

girls.java

import java.io.*;

class girls 
{
    String name;
    int age;

    girls(String name,int age)
    {
        this.name=name;
        this.age=age;
    }

    void display()
    {
        System.out.println("Name="+name+"Age="+age);
    }
 }

group.java`

    import java.io.*;


class boys
{
    int rollno;

    boys()
    {
        rollno=100;
        System.out.printf("%d",rollno);
    }
}

public class group
{
    public static void main(String args[])
    {
        boys b = new boys();

        girls g = new girls("sri divya",21);        

        g.display();
    }
}   

And here are my question after using the javac command I have only one boys.class file why? and

after the command 'java group' shows me the result of the boys class inside the file group.java and not the boys class inside the file boys.java why?

what should I do if I want output of boys class inside boys.java inspite of having boys class in group.java?

why there is no error inspite of having two same class names?

someone pls help me

Upvotes: 0

Views: 1859

Answers (2)

meskobalazs
meskobalazs

Reputation: 16041

If you name two classes the same, they should be in different packages, and when you want to use both of them in another class, you will have to reference (at least) one of them using its fully qualified name.

If two classes were in the same package with the same name, only one of them would be loaded by the ClassLoader -- which is most certainly not something you would want. This also causes an error in Eclipse, namely: the type Boys is already defined.

So if you can help it, you really should not name the classes the same way (and definitely not in the same package).

Update: I have tested the code, in this exact case the code runs even after throwing the compilation error, the Boys class is loaded by the ClassLoader, the one defined in the Group class is not. Actually, the Boys class in Groups.java is not even in the bytecode, it is removed during compilation.

PS: If you are writing Java code, you really should stick to conventions (upper camelcase classnames, camelcase methods, egyptian brackets etc.), especially if you are not the only one working on the code.

Upvotes: 1

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36401

And here are my question after using the javac command I have only one boys.class file why?

There can be only one class named boys in a given package, so the compiler will only generates one.

after the command 'java group' shows me the result of the boys class inside the file group.java and not the boys class inside the file boys.java why?

When the compiler parse your group.java file he found two class and then compile them.

what should I do if I want output of boys class inside boys.java inspite of having boys class in group.java?

Remove the definition of boys from group.java.

why there is no error inspite of having two same class names?

When you compile group.java the compiler found the boys class in it, so it don't need to look outside for its definition.

Defining a class at different places in the same package is a non-sense. Choose.


Now if you really want to replace the boys class with the one generated from the boys.java file you can:

  • compile group.java (this will generate both group.class and boys.class from it)
  • compile boys.java (this will override the old boys.class with the one of boys.java)

It will work, but I would definitely not use such a construction.

Upvotes: 2

Related Questions