Using predefined class name as Class or Variable name in Java
Last Updated :
17 Mar, 2023
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
public class Number
{
public static void main (String[] args)
{
System.out.println("It works");
}
}
|
Output:
It works
Using String as User Defined Class:
Java
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
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
public class Number
{
int Number = 20 ;
public static void main (String[] args)
{
Number Number = new Number();
System.out.println(Number);
System.out.println(Number.Number);
}
}
|
Output
Number@5a07e868
20
Similar Reads
Using _ (underscore) as Variable Name in Java
As we do know variables in java or rather in any language is introduced to write a code where it is suggested to give meaningful names to the variables as per their usage in code and especially in object-oriented languages are supposed to be used locally wherever it is possible instead of just using
3 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
Why non-static variable cannot be referenced from a static method in Java
Java is one of the most popular and widely used programming language and platform. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In java, methods can be declared as either static or non-static.
4 min read
Private Constructors and Singleton Classes in Java
In Java, the private constructor is a special type of constructor that cannot be accessed from outside the class. This is used in conjunction with the singleton design pattern to control the instantiation. Private ConstructorThe private constructor is used to resist other classes to create new insta
2 min read
Unnamed Patterns and Variables in Java
Java programming language offers multiple features that allow developers to write rich and concise code. Among these features, there are unnamed patterns and variables which enables developers to work with data in a more flexible and elegant way. In this article, we will study about unnamed patterns
4 min read
Static and non static blank final variables in Java
A variable provides us with named storage that our programs can manipulate. There are two types of data variables in a class: Instance variables : Instance variables are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot f
5 min read
Using Static Variables in Java
Here we will discuss the static variables in java. Java actually doesnât have the concept of Global variable. To define a Global variable in java, the keyword static is used. The advantage of the static variable is discussed below. Now geeks you must be wondering out what are the advantages of stati
3 min read
Class forName(String, boolean, ClassLoader) method in Java with Examples
The forName(String, boolean, ClassLoader) method of java.lang.Class class is used to get the instance of this Class with the specified class name, using the specified class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.Syntax: pu
2 min read
Myth about the file name and class name in Java
The first lecture note given during java class is âIn java file name and class name should be the sameâ. When the above law is violated a compiler error message will appear as below Java Code /***** File name: Trial.java ******/ public class Geeks { public static void main(String[] args) { System.ou
3 min read
StringWriter getClass() method in Java with Examples
The getClass() method of StringWriter Class in Java is used to get the parent Class of this StringWriter instance. This method does not accepts any parameter and returns the required Class details. Syntax: public final Class String getClass() Parameters: This method accepts does not accepts any para
2 min read