Java Naming Conventions and Identifier Names

Java Bytes #2 Code: class Main { public static void main(String[] args) { String String = "String"; System.out.println(String); } } The below will be the output... String This is a valid code because all the predefined class names in Java can be used as identifier names. Only keywords can't be used as identifiers. But it is a good practice to follow the naming conventions as below. 1. class/interface names with capital letter for each word(PascalCase). Eg: Runnable, StudentDto 2. method/variable names with small letter for the starting word followed by capital letter(camelCase). Eg: student, employeeRecord 3. constant names with full capital letters(UPPER_SNAKE_CASE). Eg: MAX_VALUE, MAX_RETRIES 4. package names with all lower case letters. Eg: java.util, java.lang What will be the output for the following code? class Main { public static void main(String[] args) { String String = "String"; String Main = "Main"; String main = "main"; String System = "System"; String out = "out"; String println = "println"; System.out.println(String); System.out.println(Main); System.out.println(main); System.out.println(System); System.out.println(out); System.out.println(println); } } If it is not giving the right output, what is the issue? ----------------- Java is like my girlfriend. I have misunderstood her many times. Still I try to understand her a lot. Please feel free to correct me if I have misunderstood her. #javaBytes #javaIsLove #javaInterviewPrep

The issue lies in out variable. It is because we have created a variable called System of type String. Now, the variable System is trying to access the variable called out which is not an instance variable of String class. To resolve the issue and print, you can specify java.lang.System for all the printing statements so that java knows that we are referring to the class called System in java.lang package. Corrected code: class Main { public static void main(String[] args) { String String = "String"; String Main = "Main"; String main = "main"; String System = "System"; String out = "out"; String println = "println"; java.lang.System.out.println(String); java.lang.System.out.println(Main); java.lang.System.out.println(main); java.lang.System.out.println(System); java.lang.System.out.println(out); java.lang.System.out.println(println); } }

Like
Reply

To view or add a comment, sign in

Explore content categories