In Java, you can use any valid identifier as a class or variable name. However, it is not recommended to use a predefined class name as a class or variable name in Java.
The reason is that when you use a predefined class name as a class or variable name, you can potentially create confusion and make your code harder to read and understand. It may also lead to errors when trying to use the predefined class in your code.
For example, let’s say you want to create a class named String to represent a string of characters in your application. This is not recommended because String is a predefined class in Java that is used to represent a string of characters. If you use String as a class name, it may confuse other developers who are working on your code, as well as the compiler, which may not be able to differentiate between your class and the predefined String class.
Similarly, it’s not recommended to use predefined class names as variable names, as this can also lead to confusion and errors. For example, if you use String as a variable name, it may be unclear whether you’re referring to the String class or a variable that holds a string value.
To avoid these issues, it’s best to use descriptive names for your classes and variables that clearly indicate their purpose and do not conflict with predefined class names.
Sure, here’s an example of using a predefined class name as a class name in Java:
Java
public class String { public static void main(String[] args) { String s = "Hello World" ; System.out.println(s); } } |
Output:
Hello World
Advantages:
There are no real advantages to using a predefined class name as a class name in Java.
Disadvantages:
- Using a predefined class name as a class name can lead to confusion and errors.
- It can make your code harder to read and understand.
- It can cause problems when trying to use the predefined class in your code.
- It can also cause problems when other developers try to work with your code.
- In the example above, we’ve used String as the class name, which is a predefined class in Java that represents a string of characters. This can cause confusion for other developers who may be working on your code, as well as the compiler, which may not be able to differentiate between your class and the predefined String class.
Overall, it’s best to avoid using predefined class names as class names or variable names in Java to prevent confusion and errors in your code.
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
Java
// 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:
Java
// 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 .
Java
// 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.
Java
// 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 = 20 ; 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); } } |
Number@5a07e868 20
This article is contributed by Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.