Author
Constructor and Initializer block
Naresh Chaurasia
Ranch Hand
Joined: May 18, 2005
Posts: 309
While going through Khalid Mughal, I came across the following statement
Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.
Why is it that constructors and initializer blocks are not members of a class?
SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
. . . because that is how the language was designed. Try searching for the history of Java and see whether that explain any more.
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
I think, they belong to objects rather than classes!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
And instance fields (which are members) do not?
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Prabhakar Reddy Bokka
Ranch Hand
Joined: Jul 26, 2005
Posts: 189
What about static variables and methods?
SCJP 5, SCWCD 5
Naman Patidar
Greenhorn
Joined: Oct 03, 2008
Posts: 15
1. Constructor declarations are not members. They are never inherited.
2. The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors. (you can check this by decompile a class having intializer block.)
Prabhakar Reddy Bokka
Ranch Hand
Joined: Jul 26, 2005
Posts: 189
Static variables and methods also cannot be inherited. They are specific to that class.
So, are they not members of the class?
In that case what all called as members of class and what not?
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Class Body and Member Declarations . In other words, all fields, methods, nested classes and nested interfaces are members. Instance and static initializers and constructors are not.
Also, not all members can be overridden. Fields and nested classes / interfaces are members but cannot be overridden.
Prabhakar Reddy Bokka
Ranch Hand
Joined: Jul 26, 2005
Posts: 189
Instance and static initializers and constructors are not.
Is there any specific reason for not calling them as members of class? Obviously inheritance is not a reason.
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Jul 27, 2010 06:31:18
0
They're just not--did you read the link provided?
And why isn't inheritance not a reason? Seriously--look at the link. Sections 8.1.6 and 8.2 address this *directly*.
Prabhakar Reddy Bokka
Ranch Hand
Joined: Jul 26, 2005
Posts: 189
They're just not--did you read the link provided?
Yeah. Now i got the things. "They're just not". I think there is no reason for that.
And why isn't inheritance not a reason? Seriously--look at the link.
Inheritance cannot be a reason because all the class members cannot be inherited to the sub classes, like static/private variables and methods.
Rob Prime wrote: Also, not all members can be overridden. Fields and nested classes / interfaces are members but cannot be overridden.
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
They cannot be overridden but they are most definitely inherited (although you can't access them if they're private, they're still there).
subject: Constructor and Initializer block