Variables only get memory allocation during object creation, so why does assigning a value to a variable outside a constructor produce no error? Moreover what is the location of this assigned value as no particular object is created?
-
Field initializers are run before the body of the constructor, that's all. Note that the memory allocation itself happens once, before *any* constructors are executed. – Jon Skeet Dec 27 '16 at 20:32
-
You'll have ask [James Gosling](https://en.m.wikipedia.org/wiki/James_Gosling) next time you see him as to why this language choice was made. – Bohemian Dec 28 '16 at 02:09
-
why my question is put on hold? @Bohemian . I am not asking why this rule is in JAVA, just want to know what is the location of the initialized value? Plz try to understand my question before putting on hold. – Rony Parker Dec 31 '16 at 04:47
-
2@Rony No, your title asks *why*. That is opinion-based, because you're essentially asking *why did the language designers make that decision*. You'll have to ask them to find out. The effect of the various forms of initialiation are all clearly documented in the [JLS](https://docs.oracle.com/javase/specs/jls/se8/html/index.html). – Bohemian Dec 31 '16 at 04:49
-
I have reframed it , hope now it will have no issues. @Bohemian – Rony Parker Dec 31 '16 at 04:57
-
I don't think this is opinion-based. He's asking about how memory allocation works in Java. – Peter Hall Dec 31 '16 at 05:40
-
See also [Are fields initialized before constructor code is run in Java?](http://stackoverflow.com/questions/14805547/are-fields-initialized-before-constructor-code-is-run-in-java) – Bohemian Dec 31 '16 at 14:12
-
I have gone through the duplicate link but it doesn't answer my question. What are you up to @Bohemian ?every time trying to block. If you have the answer write it down rather than bothering me.It's not the way to stop someone everytime. – Rony Parker Dec 31 '16 at 20:49
-
@rony what about the link in my previous comment? – Bohemian Dec 31 '16 at 20:54
-
No, the location part is not asked anywhere. @Bohemian – Rony Parker Dec 31 '16 at 21:01
-
@rony fine I've reopened it. FYI the "location" is a JVM implementation choice and its actual memory location can not be referenced a la C, and in fact is allowed to change at any time. The memory footprint of the object is "allocated" at creation. How the values for the various "locations" are assigned is established by a well defined order as per the previous duplicate. – Bohemian Jan 01 '17 at 00:20
-
Personally, I don't understand the wave of downvotes. Someone wants to know how Java allocations are performed. So what's the issue here? – scottb Jan 01 '17 at 00:44
-
"Variables only get memory allocation during object creation, so why does assigning a value to a variable outside a constructor produce no error?" - this is a non-sequitur; why should the moment when memory allocation is performed affect the legality of assigning a value, as long as the former occurs before the latter? If you assign to an instance variable, the object has necessarily already been created, hence the memory for the variable has already been allocated. Why _should_ you get an error? – davmac Jan 01 '17 at 00:47
-
And: "as no particular object is created" - doesn't make sense; you cannot assign an instance variable _unless_ the instance _has already_ been created. This would be a much better question if you gave some example code which you think is problematic. – davmac Jan 01 '17 at 00:49
-
When defining a class we can initialize instance variables with some values outside the constructor. It gets compiled without throwing any error message. My question is if no object is created now, so where is the initialized value being saved? – Rony Parker Jan 01 '17 at 07:39
-
@RonyParker you mean field initialisers? These are executed when an object is constructed, not before. – davmac Jan 01 '17 at 11:25
-
I know that @davmac. That means initializing instance variable outside constructor is same as initialing inside constructor? – Rony Parker Jan 01 '17 at 19:42
-
@RonyParker field initializers have a similar effect to initializations performed within the constructor, yes. (But that is only one case of "initializing instance variable outside constructor" - you could be doing it inside a method, which is outside the constructor; that won't have the same effect at all). – davmac Jan 01 '17 at 20:20
1 Answers
Because all of the instance field initializer code (and any instance initializer blocks) are executed as if they were part of the constructor.
Variables only get memory allocation during object creation
In fact, the sequence is as follows:
- Evaluate constructor argument expressions.
- Perform class initialization if it hasn't already been performed. (This happens once in the lifetime of the class ...)
- Allocate the memory for the object, including the memory for the instance fields of the class and superclass chain.
- Perform default initialization the fields.
- Perform the
this(...)
orsuper(...)
call. (This recurses up the chain performing field initialization and running constructors. For athis
call, we then skip the next step.) - Execute the classes instance field initializers and static initializer blocks, in source code order.
- Execute the remainder of the constructor.
- If this was the leaf constructor, return the constructed object. Otherwise unwind one level of recursion.
Step 6 is where the field initializers you were concerned about get dealt with. The code for the initializer expressions and blocks are combined into a synthetic method that gets called by the constructor at the appropriate point. However, that is an implementation detail.
As you can see, the amount of memory allocated to the object is determined very early on, and is not affected by any of the initialization logic. The variables and the amount of memory needed to represent them in the object itself will be the same ... no matter what.
Moreover what is the location of this assigned value as no particular object is created?
The locations of the fields are part of the object allocated in step 3. Just like the case where the initialization was done in the constructor.
(Perhaps you were confused about what actually happens when you assign an object or array to a field? Remember that Object and Array types are called reference types. A field or variable whose type is a reference type corresponds to a location to hold just the reference ... NOT the actual state of the object that the reference refers to.)
(Perhaps you were thinking that the invocation of a constructor is the same as the invocation of a normal method. That isn't so ... see above.)

- 698,415
- 94
- 811
- 1,216