This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor invocation" Watch "constructor invocation" New topic
Author

constructor invocation

Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
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
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Luckson Karikoga
Greenhorn

Joined: Apr 27, 2010
Posts: 13
Thanks Henry.

Any additional reference material I can look out for.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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

Joined: Apr 27, 2010
Posts: 13
Thanks Henry.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Apr 27, 2010
Posts: 13
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
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor invocation
 
Similar Threads
Accessing static method with super
static inner class
Overridding
confusion in static
Test your knowledge