• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor invocation

 
Luckson Karikoga
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can anyone please explain why static members can be accessed as part of the call to super() or this().

Example: super(Animal.NAME) assuming NAME is declared as a static variable.

From my own understanding constructors have to construct an actual object. And static members belong to the actual class.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luckson Karikoga wrote:Hello,

Can anyone please explain why static members can be accessed as part of the call to super() or this().

Example: super(Animal.NAME) assuming NAME is declared as a static variable.

From my own understanding constructors have to construct an actual object. And static members belong to the actual class.


Why not? The class should be loaded and initialized by the time an instance is created (actually, it is possible for an instance to be created before class initialization has been completed, but let's not quibble over details). The point is, how is it different than using other instances, method local variables, or any other information needed for initialization?

Henry
 
Luckson Karikoga
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.

Any additional reference material I can look out for.

 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luckson Karikoga wrote:
Any additional reference material I can look out for.


Not sure what reference material you are looking for... Your question was "why can I access this data"? And my response was "why not? the data is in scope and accessible (permitted)".

It doesn't really take much documentation to know that if something is in scope and accessible, then it can be accessed.

Henry
 
Luckson Karikoga
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other classes can access static members of a class without creating an instance of the class (assuming those members aren't private). Why would there be greater restrictions accessing them from within the class?
 
Luckson Karikoga
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well where I was failing to understand is that you are not allowed to call a constructor using an instance member.
Example: super(nonstaticMember()); but you are allowed:
super(Xxx.staticMember()).
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luckson Karikoga wrote:Well where I was failing to understand is that you are not allowed to call a constructor using an instance member.
Example: super(nonstaticMember()); but you are allowed:
super(Xxx.staticMember()).


The requirement is defined in the Java Language Specification ...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.7.1

The exact wording is...

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.


Unfortunately, I am not sure of the reason for the restriction though. As a speculation, I am thinking that it is related to preventing using instance resources before they are initialized; however, if that is the purpose, it doesn't really work well.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic