This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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 Head First Android 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: 8898
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);
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Is the program given wrong in K&B Book?
It's not a secret anymore!