-3

My question is - When I print value of the String name, it's null(which is understandable). But, is it because of JVM default constructor or because of the one I provided? What I assume is if I provide constructor, it will be called and JVM will not call the default constructor. But my constructor is empty so who sets null for string?

class Animal{

        String name;
        //no-arg constructor I provided which does nothing
        Animal(){
        }

        void printName() {
            System.out.println("name: "+name);
        }

        public static void main(String[] args) {

            Animal cat = new Animal(); 
            cat.printName()
        }
   }
Y.R
  • 210
  • 2
  • 13
  • Nothing here makes sense. 1. The *compiler* adds default constructors. Not the JVM. 2. If you add *any* constructor, the compiler won't add the default constructor. 3. You can't have two constructors wth the same signature. 4. Your constructor does nothing, so why are you wondering whether *another* constructor that does nothing has been added? 4. Your constructor doesn't initialize `name`, so why are you wondering why `name` isn't initialized? – user207421 Sep 10 '17 at 00:10
  • No, I am not wondering so many things as you suggested. I know the constructor I added does nothing and surely doesn't initialise name. If you read my question properly it clearly says string is null and its understandable to me why. What I wanted to know was when and who gives default value when my constructor does nothing. Why I wanted to know? Because only if you wonder you learn. Anyway, It has been answered below by JB Zinet. – Y.R Sep 10 '17 at 05:27
  • You are explicitly wondering whether the compiler adds an extra, duplicate, non-conforming constructor. It is what your question is about, stated in both the title and the body of the question. It doesn't happen, and it doesn't make sense. – user207421 Sep 10 '17 at 09:47

1 Answers1

3

The compiler only adds a no-arg constructor if you don't have any constructor defined in the class. You can't have two constructors with the same signature anyway, son adding a second no-arg constructor wouldn't make sense.

Your name is null because fields are always assigned a default value if the code doesn't assign anything to them, and the defaut value for reference types is null. It's 0 for primitive numeric types, and false for booleans.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So you mean value of string was set default and my constructor was called but it did nothing. Right? If name is null because fields are always assigned default value(but when?), then what's the use of default constructor JVM provides? I thought JVM provide default constructor and it assigns default values. So, my question is who assigns defaults here in this code and when? Thanks – Y.R Sep 09 '17 at 23:37
  • The use of the default constructor is to be able to create instances of your class. How could you create an instance if the class had no constructor? See https://stackoverflow.com/questions/22368106/are-default-field-values-assigned-by-the-compiler-or-the-jvm for your question. – JB Nizet Sep 09 '17 at 23:43