wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

Is the program given wrong in K&B Book?

balajee annamalai
Ranch Hand

Joined: May 09, 2007
Posts: 31
Hi,
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
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
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?

Thanks,

Bert


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

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
Greenhorn

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

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

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

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);
changeIt(size);
System.out.println("size after changeIt is " + size);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
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