aspose file tools*
The moose likes Beginning Java and the fly likes Sharpen your pencil, chapter numbers and statics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sharpen your pencil, chapter numbers and statics" Watch "Sharpen your pencil, chapter numbers and statics" New topic
Author

Sharpen your pencil, chapter numbers and statics

Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
Hey guys, I had doubt regarding Sharpen you pencil - head first java page 285.
We need to check which of the following would compile or not! I have commented where i feel there might be a problem.

please tell me , if i am right!
And what are the things i need to look for?

Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Welcome to the Ranch! Please UseCodeTags when you post source code, and make sure the indentation is preserved - that way your source code is much easier to read.

1. Right. Non-final member variables (static or not) are initialized to default values if you don't initialize them explicitly. For int the default value is 0, so x will be 0.

2. Correct.

3. Correct - final variables must be initialized explicitly. They are not initialized with default values.

4. Nothing wrong with that.

5. Also no problem. Note that the argument variable (lower-case x) doesn't have anything to do with the member variable upper-case X.

6. This one is wrong. The argument variable x hides the member variable x. The static method is not trying to access the member variable!

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
Thanks, so much for the answers..

Yes, i am new to the forum. i will use the code tags from now on.

So even the last one will compile?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Why don't you just try it out?
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
Actually i just wanted to have a fair reasoning behind all and then try compiling..

Thanks a lot for your help.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

I agree, if you want to learn and test yourself to try to solve it without the computer first, but afterwards you can check if you were right by trying it out.

The last example should just compile normally. The argument variable hides the member variable. So inside the method, x refers to the argument variable, and not the member variable. If you want to access the member variable inside the method, you can prefix it with "this":
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
Wow, you just made so clear !
Thanks a bunch !
Matt Hanrahan
Greenhorn

Joined: Jul 09, 2011
Posts: 14

I just wanted to say thanks also! #6 was where my question was also. The bottom line I'm picking up here is that a static method's argument variable can "take" or "access" a non-static variable. Did I say that right? Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Matt Hanrahan wrote:... a static method's argument variable can "take" or "access" a non-static variable. Did I say that right? Thanks.

Not really... when you have a method with an argument that has the same name as a member variable, then inside the method, the argument is used instead of the member variable. So, the argument hides the member variable:

This doesn't really have anything to do with whether the method is static or not. A static method can never access non-static member variables.

Note: I made a mistake in my post above from 20 February. You can't use "this.x" in a static method, because "this" (the object that the method is being called on) doesn't exist in a static method.

And welcome to the Ranch!
Matt Hanrahan
Greenhorn

Joined: Jul 09, 2011
Posts: 14

Thank you for the help.



So in this case inside the method the print statement gets "x" from the argument and the argument gets "x" from the member?
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
So in this case inside the method the print statement gets "x" from the argument and the argument gets "x" from the member?


Not exactly.

'x' in the argument takes its value from the calling code as shown. It is an extended example of shadowing where the argument variable shadows the member variable.



Jitendra
SCJP1.5
SCWCD1.5
Matt Hanrahan
Greenhorn

Joined: Jul 09, 2011
Posts: 14

Awesome and thank you. I was still not getting it exactly so I added a a main and tried to run the code.
I'm adding the two samples for anyone who has this same question as an example.
Let me know if I got anything wrong, but I think this helps to illustrate the point (which I think is an important one).

This code:

Will compile and output:
19

This code:

will not compile.

The compiler returns; "non-static variable x cannot be referenced from a static context"

Thanks again for the help!
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
The code on line five(5) works because, you are trying to print an argument variable which has scope and priority inside the method.
But the code on line 10 fails to compile because it is trying to use the variable on line 2 which is non static and hence cannot be referenced from the static context(main method).
Matt Hanrahan
Greenhorn

Joined: Jul 09, 2011
Posts: 14

Indeed! And thanks for the quick reply and help!
Nikhil Kadi
Greenhorn

Joined: Jun 11, 2013
Posts: 2
hello all java loves, just joined the forum. I am reading head first java and reached this exercise everyone is taking about.

I don't understand in 3rd code I.e foo3, if final variables need to be assigned explicitly, how is it correct and if correct what will be the output. thanks. will be visiting often.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Hello Nikhil,
Welcome to Java Ranch.
In class Foo3 if there was no final keyword then it would have compiled.
The final keyword states that the variable cannot be assigned any other value later.. Its like the gospel truth, cant change the variable value at all.
Now if you dont initialise the variable declared as final, JVM is surely going to cry foul play.
Hope this reasoning helps


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Nikhil Kadi
Greenhorn

Joined: Jun 11, 2013
Posts: 2
thanks a lot for the quick reply. it removes my confusion. it's just that it was answered correct in a previously reply, probably by mistake, which confused me. thanks a lot again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sharpen your pencil, chapter numbers and statics
 
Similar Threads
Inner class doubt
why does value not change???
final variable
Foo6 compilation