Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Sharpen your pencil, chapter numbers and statics

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just try it out?
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, you just made so clear !
Thanks a bunch !
 
Greenhorn
Posts: 14
Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14
Netscape
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


 
Matt Hanrahan
Greenhorn
Posts: 14
Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed! And thanks for the quick reply and help!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nikhil Kadi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please show me how I can test these out? I'm having trouble doing it with Eclipse right now.. Thanks
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Sung wrote:Can someone please show me how I can test these out? I'm having trouble doing it with Eclipse right now.. Thanks


Compile from the command line.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic