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

  1. 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 classfilter_none

    edit

    play_arrow

    brightness_4

    // 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:filter_none

    edit

    play_arrow

    brightness_4

    // 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 .

    filter_none

    edit

    play_arrow

    brightness_4

    // 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.

filter_none

edit

play_arrow

brightness_4

// 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

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.

 

Related Articles

post a comment