• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rules for constructors - p. 316 from Bert and Kathy's book

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused about the rule which states: "You can access static variables and methods, although you can use them only as part of the call to super() or this(). (Example: super(Animal.DoThings()) )"
Why does the following code compile okay (p.327 - Exam Cram):
class Widget extends Thingee
{
static private int widgetCount = 0;
public String wName;
int wNumber;
private static syncronized int addWidget()
{
return ++widgetCount();
}
public Widget()
{
wNumber = addWidget();
}
}
According to this rule, shouldn't the compiler complain about the addWidget call of a static method in the Widget constructor?
Mansi
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Non-static method can always access non-static and static members.Since non-static methods are always having implicit this reference so they can directly access other static members
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mansi: I agree with you. The wording seems to be wrong.
Bert or Kathy: What you do you guys say?
Thanks
Barkat
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wNumber = addWidget(); implies wNumber = this.addWidget();
 
Mansi Vyas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Johannes,
addWidget() method is defined as static, so how can u use it in conjuction with this?
Mansi
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mansi -
Dang! You found an error. We think that our fingers were detached from our brains when we wrote that bullet point :roll:
How about if it read like this?
Only static variables and methods can be accessed as part of the call to super() or this().
Thanks for a good catch!
-Bert
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert you changed the whole meaning. You now state that super() and this() can ONLY access static variables.
As for my answer Mansi, it looks like I need to study much much more before taking my exam. I was under impressession that this() and the this operater where the same thing !!!. My applogies
 
Mansi Vyas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert,
This topic is from some time ago, but I had another question. What about the paramerters passed into the constructor? Shouldn't super() and this() also be allowed to access these parameters?
Mansi
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic