• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Mistake(I think) on K&B's 1.4 book

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,

from K&B's 1.4 book: under chapter 3, under Passing variables into methods section they have described about shadowing concept. there we have the code:


Here 'size' is not a static variable, but they are accessign in static context. Is this wrong or right somehow?

Thanks.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right, "size" cannot be accessed in:
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, the object reference f should be attached to the variable size, that's object created for. I think the part of the code meant to be like this:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes u r correct

static code cannot be accessed in non static block
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is already in the errata:


[sandeep]: static code cannot be accessed in non static block

Other way around. Static code can be accesed in a non-static block. However non-static code cannot be directly accessed from a static block. Unless you create an instance and use that to access the nonstatic code.
 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just I asked to clarify my dowt, I dont mean to point out the error.
Initially I thought this program is wrong, but again I thought there might be some hidden concept behind this, I might be wrong because its from great authors; so I posted here. Now I understood, it was printign mistake, everyone commits mistakes sometimes

Sorry, if I am referrig to the one which already exisiting in Errata.

Thanks for your replies folks.
 
That is a really big piece of pie for such a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic