• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor and Initializer block

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, they belong to objects rather than classes!
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And instance fields (which are members) do not?
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about static variables and methods?
 
Naman Patidar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They cannot be overridden but they are most definitely inherited (although you can't access them if they're private, they're still there).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic