Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #611
| From | Lew <noone@lewscanon.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Why can one invoke setters and getters from a (class) constructor? |
| Date | 2011-04-23 13:58 -0400 |
| Organization | albasani.net |
| Message-ID | <iov40m$hcr$1@news.albasani.net> (permalink) |
| References | <87bozwkgz8.fsf@merciadriluca-station.MERCIADRILUCA> |
Merciadri Luca wrote: > ... I'm wondering why > I can invoke (i.e. use) setters and getters in a (class) > constructor. Setters and getters are defined over the object whose > type is defined by the class. But the object is only defined once the > constructor has been parsed totally, isn't it? Nn. If that were true there'd be no object on whom to invoke a constructor body. The answer is here: <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2> <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.6> and especially <http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.5> Note also that "The default initialization of any object happens-before any other actions (other than default-writes) of a program." <http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4.5> In an nutshell, the object exists before initialization can occur. The methods are defined *in the class*, not in the object. The state on which they operate is instance state, but the methods exist in the class definition before any instantiation can occur. So the sequence is: The class definition, including instance methods, is loaded. Class state is initialized. "Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (§8.3). "... all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§4.12.5). "Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:" [JLS §12.5] - 'this' or (implicit or explicit) 'super' constructor invocation - instance and instance variable initializers, left-to-right - constructor body -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Why can one invoke setters and getters from a (class) constructor? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 19:36 +0200
Re: Why can one invoke setters and getters from a (class) constructor? Lew <noone@lewscanon.com> - 2011-04-23 13:58 -0400
Re: Why can one invoke setters and getters from a (class) constructor? markspace <-@.> - 2011-04-23 11:00 -0700
Re: Why can one invoke setters and getters from a (class) constructor? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 23:53 +0200
Re: Why can one invoke setters and getters from a (class) constructor? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-04-23 20:36 -0400
Re: Why can one invoke setters and getters from a (class) constructor? Lew <noone@lewscanon.com> - 2011-04-23 22:37 -0400
Re: Why can one invoke setters and getters from a (class) constructor? Roedy Green <see_website@mindprod.com.invalid> - 2011-04-24 06:53 -0700
Re: Why can one invoke setters and getters from a (class) constructor? Fred <fred.l.kleinschmidt@boeing.com> - 2011-04-25 11:06 -0700
Re: Why can one invoke setters and getters from a (class) constructor? Lew <noone@lewscanon.com> - 2011-04-25 15:14 -0400
csiph-web