Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is the program given wrong in K&B Book? 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 "Is the program given wrong in K&B Book?" Watch "Is the program given wrong in K&B Book?" New topic

Is the program given wrong in K&B Book?

balajee annamalai
Ranch Hand

Joined: May 09, 2007
Posts: 31
Im preparing for SCJP1.4 and in Chapter 3 of Operators and assignments "From the classroom" section.They have given an example about shadowing of variables.

In the above example they are saying the output also but how will this code gets compiled beacuse size is an non static varaiable and we can't use that in the static main.
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
you are right code wont compile. cant access non-static instance var from static main.
Bert Bates

Joined: Oct 14, 2002
Posts: 8806
Hi Balajee,

The error you're talking about is very, very, old. Do you have a hard copy of the book, or are you using a pdf that you borrowed from someone?



Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Carl Cordova

Joined: Feb 02, 2008
Posts: 4
Maybe I am tired, but is size on line# 2 a Class variable and not an instance?

Carl Cordova
Kshitija Mitter

Joined: Mar 03, 2008
Posts: 11
The variable on line 2 in above cntext is Instance varibale

Hope this link will be useful.
[ May 28, 2008: Message edited by: Kshitija Mitter ]
Carl Cordova

Joined: Feb 02, 2008
Posts: 4
From page 207 of K&B, line 2?

class Foo {
static int size = 7;
static void changeIt(int size) {
size = size + 200;
System.out.println("size in changeIt is " + size);

public static void main (String [] args) {
Foo f = new Foo();
System.out.println("size = " + size);
System.out.println("size after changeIt is " + size);
jQuery in Action, 2nd edition
subject: Is the program given wrong in K&B Book?
Similar Threads
shadowing an variable
Mistake(I think) on K&B's 1.4 book
Shadowing Variables
An error in K&B certification book
Shadowing Variables