• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

constructor invocation

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.

Any additional reference material I can look out for.

 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
please buy this thing and then I get a fat cut of the action:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic