Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Initial value of an object  RSS feed

 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following example:



Did I understand correctly what's going on here?

object1:
   1) a new object is created
   2) its field are initialized to the default values (because of the default constructor)
   3) reference to the created object is returned to the variable object1

object2:
   1) variable object2 is declared
   2) as it's not pointing to any object, a null reference is assigned to object2

object3:
   here happens the same thing as with object2, however, null reference is assigned explicitly



 
Dave Tolls
Rancher
Posts: 2909
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
object2 is uninitialised.
If you attempted to use it:

The compiler will complain that it hasn't been initialised.
 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dave.

If I had an unitialized array like in the following example



array1 would also point to nothing instead of pointing to null?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksandra Pestova wrote:
array1 would also point to nothing instead of pointing to null?


Well, technically, since the "error" is a compiler error. And hence, executable code (and the local variable) should not exist, does it really make sense to say what it points to?

Henry
 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I got it what I didn't understand.

I saw a similar example with an array, but with an array INSTANCE variable, not the local one.
Later on when I tried to play around with that example I didn't pay attention that I was using a local array.

So if the example were like this:



then array1 would point to null I guess.

Thanks, for your reply, Henry.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksandra Pestova wrote:. . .
object1:
   1) a new object is created
   2) its field are initialized to the default values (because of the default constructor)
   3) reference to the created object is returned to the variable object1
Correct. Remember the default constructor does not initialise any fields. You also know the correct meaning of “default constructor”.
object2:
   1) variable object2 is declared
   2) as it's not pointing to any object, a null reference is assigned to object2
As DT has implied, that is not correct, I am afraid. It doesn't point to an object and it doesn't point to null. It might even point to whatever was on the stack previously, and who knows what that might be. So as DT says, the compiler will not allow you to use that reference until it appears left of an assignment operator. It must be definitely assigned, as the Java® Language Specification (=JLS) calls it. This part of the JLS will tell you all about it, but the JLS is usually very difficult to understand.


object3:   here happens the same thing as with object2, however, null reference is assigned explicitly
Correct.
 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also know the correct meaning of “default constructor”


The default constructor is the one, which doesn't have arguments. But if there's no constructor in the program at all, then the compiler automatically creates a default constructor. Right?

Remember the default constructor does not initialize any fields.


Now I am slightly confused.

If a new object is created, I thought it's the purpose of the constructor to provide the instance variables with the default values.
 
Stephan van Hulst
Saloon Keeper
Posts: 7797
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksandra Pestova wrote:The default constructor is the one, which doesn't have arguments. But if there's no constructor in the program at all, then the compiler automatically creates a default constructor. Right?

No. The default constructor is the one that is automatically inserted by the compiler if you don't declare any constructors. It has no arguments, but if you yourself declare a constructor with no arguments, that constructor is NOT referred to as the 'default constructor'.

If a new object is created, I thought it's the purpose of the constructor to provide the instance variables with the default values.

No, variables already have a default value. The task of the constructor is to give variables a value other than the default. The default constructor doesn't do anything, other than call the super-class constructor.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[critchie@... ~]$ javac Foo.java
[1]+  Done                    gedit Foo.java
[critchie@... ~]$ javap -c Foo
Compiled from "Foo.java"
public class Foo {
  public Foo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
}
That shows the bytecode of the Foo.class file. Now, if we add a constructor to the .java file, it will look like this.Now, observing the bytecode with the javap tool we see this:-
[critchie@... ~]$ javac Foo.java
[1]+  Done                    gedit Foo.java
[critchie@... ~]$ javap -c Foo
Compiled from "Foo.java"
public class Foo {
  public Foo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
}
See: no difference at all. Both the default constructor seen first and the explicit constructor in the second case do nothing but call the superclass constructor, which has been changed to read "<init>". No sign of the fields being reassigned. If I want the int to have the value 123, I must write that explicitly, otherwise how is the javac tool to guess the value I want?

So, we learnt:
  • 1: A default constructor is only created when the user doesn't write their own constructor.
  • 2: It behaves exactly the same as
    xxx Foo()
    {
        super();
    }
    where xxx is the same access modifier as the class (I think).
  • If you are feeling brave, you can read about it in the Java® Language Specification. This link about the javadoc tool and comments says it is bad style to have a default constructor.
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems that "default constructor" is very often misused on the web. I have seen the same wrong definition several times.
    Thanks for clarifying that.

    As far as I understand the superclass of the class Foo is java.lang.Object. Then the implicit default constructor calls the constructor of the class Object.
    It's still unclear to me where do the fields i and s get their default values from.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7797
    142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Non-final fields always get assigned a default value if you don't initialize them explicitly when you declare them.

  • Numeric primitives have a default value of 0.
  • Booleans are false by default.
  • Reference types are null by default.
  •  
    Henry Wong
    author
    Sheriff
    Posts: 23283
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aleksandra Pestova wrote:
    It's still unclear to me where do the fields i and s get their default values from.


    Interestingly, the JLS doesn't actually say where either (unless someone noticed something that I missed). It just states that it must be initialized to default values. Considering all the default values are zeros, I will speculate, that as an implementation detail, that the JVM does a block zero of the memory that it allocates for the object.

    Henry
     
    Junilu Lacar
    Sheriff
    Posts: 11125
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    See the JLS https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all for the help!
    I realized that I misunderstood some concepts, but thank you for clarifying that.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!