How to use predefined class name as class or variable name in java

In Java, it is allowed to use predefined class names as class or variable names. However, according to the Java specification language (section 3.9), the basic rule of naming in Java is that you cannot use keywords as class names, variable names, or folder names for packages. Using any of the predefined classes in Java will not cause a compiler error, because 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

1. Question :

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

2. 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 the main thread is looking for a main method() with a predefined String array parameter. But here, it got the main method() using the user-defined String class. Whenever the main thread sees a class name, it will try to search for the class scope by scope. First it will be seen in your program and then in your bag. If it is not found, then the JVM follows the principle of delegation hierarchy to load that class. Therefore you will get runtime errors. In order to run the above program, we can also provide the full path of the String class, namely 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 = 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);
	}
}

Output:

Number@15db9742
20

 

Submit Your Programming Assignment Details