utkarsh
utkarsh

Reputation: 447

How can we create an object with same name as its class?

How can we use same name for class and object , without any issues from the compiler ?

class A{
    public static void main (){
        A A = new A ();
    }
}

Or

class A{
    public static void main(){
        String String="java";
        System.out.println(String);
    }
}

How does the java compiler handle this?

Upvotes: 3

Views: 3242

Answers (1)

Crazyjavahacking
Crazyjavahacking

Reputation: 9697

In Java it is absolutely correct to specify the name of the class and the name of the variable to be exactly the same. The compiler understands the syntax so it knows what part of the program represents the identifier and what part represents the class name.

But you should never do it as it will become a readability nightmare. Absolutely misleading and violating Java Naming Conventions.

Upvotes: 3

Related Questions