• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

pg 316 - using static from within constructor

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I'm looking at this statement from pg 316 of
Kathy & Bert's book. It says:
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()))
I don't get it. I can access static vars and
methods in my constructors. Is anyone else
confused by this? Is it a mistake in the book?
Or am I misinterpreting it?
Thanks,
Pete
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Kleczka:
Hi all.
I'm looking at this statement from pg 316 of
Kathy & Bert's book. It says:
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()))
I don't get it. I can access static vars and
methods in my constructors. Is anyone else
confused by this? Is it a mistake in the book?
Or am I misinterpreting it?
Thanks,
Pete


I think that they are contrasting this point to the previous one:

You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.


I have the book, but I'm going through Just Java 2 before I tackle this one.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Kleczka:
Hi all.
I'm looking at this statement from pg 316 of
Kathy & Bert's book. It says:
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()))
I don't get it. I can access static vars and
methods in my constructors. Is anyone else
confused by this? Is it a mistake in the book?
Or am I misinterpreting it?
Thanks,
Pete


-------------------------------------------------
Hi Pete,
Look at the following code. This code is from Ksierra book from p321.


Object constructor is not finished until all super class object consturctors are finished. so at the point you call the object itself is not created. so only method it can access is the static variables and static methods.
In Above Code if you remove static keyword from line1 then it will give you compiler error

I hope it helps.
Priyank
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic