Using predefined class name as Class or Variable name in Java

Course Curriculum

Using predefined class name as Class or Variable name in Java

Using predefined class name as Class or Variable name in Java

In Java, Using predefined class name as Class or Variable name is allowed. However, According to Java Specification Language(§3.9) the basic rule for naming in Java is that you cannot use a keyword as name of a class, name of a variable nor the name of a folder used for package.
Using any predefined class in Java won’t cause such compiler error as Java predefined classes are not keywords.

Following are some invalid variable declarations in Java:

boolean break = false; // not allowed as break is keyword
int boolean = 8; // not allowed as boolean is keyword
boolean goto = false; // not allowed as goto is keyword
String final = "hi"; // not allowed as final is keyword
Using predefined class name as User defined class name

Question : Can we have our class name as one of the predefined class name in our program?
Answer : Yes we can have it. Below is example of using Number as user defined class

// Number is predefined class name in java.lang package
// Note : java.lang package is included in every java program by default
public class Number
{
public static void main (String[] args)
{
System.out.println("It works");
}
}
Output:

It works
Using String as User Defined Class:

// String is predefined class name in java.lang package
// Note : java.lang package is included in every java program by default
public class String
{
public static void main (String[] args)
{
System.out.println("I got confused");
}
}
However, in this case you will get run-time error like this :

Error: Main method not found in class String, please define
the main method as:
public static void main(String[] args)
Explanation : This is because Main thread is looking for main method() with predefined String class array argument. But here, it got main method() with user defined String class. Whenever Main thread will see a class name, it tries to search that class scope by scope. First it will see in your program, then in your package.If not found, then JVM follows delegation hierarchy principle to load that class.Hence you will get run-time error.
To run above program, we can also provide full path of String class, i.e. java.lang.String .

// String is predefined class name in java.lang package
// Note : java.lang package is included in every java program by default
public class String
{
public static void main (java.lang.String[] args)
{
System.out.println("I got confused");
}
}
Output:

I got confused
Using predefined class name as User defined Variable name

Question : Can we have a variable name as one of the predefined class name in our program?
Answer : Yes we can have it.

// Number is predefined class name in java.lang package
// Note : java.lang package is included in every java program by default
public class Number
{
// instance variable
int Number = 30;

public static void main (String[] args)
{
// reference variable
Number Number = new Number();

// printing reference
System.out.println(Number);

// printing instance variable
System.out.println(Number.Number);
}
}
Output:

Number@25db9942
30

Microservices Introduction (Prev Lesson)
(Next Lesson) Java Identifiers