This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Developer Career Masterplan: Build your path to senior level and beyond with practical insights from industry experts and have Heather VanCura and Bruno Souza on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Compilation problem using static and final modifiers

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Head First Java book there is a compilation exercise using the five code examples below, using my Java IDE none of them compile (surely some have to ?), and I'm lost as to why. I'd appreciate greatly any pointers on where I'm going wrong here, I get the same compilation warning for each so I've checked the syntax for errors and they all seem to be fine. Thanks.




Compilation warning: Cannot find symbol



Compilation warning: cannot find symbol.

Is this failing to compile because x hasn't been initalized ?



Compilation warning: cannot find symbol.

Is this because of the static declaration on x and the method invocation of x isn't calling on the class itself ??



Compilation fails again here, is this anything about passing a variable into two methods ?



Compilation fails !
 
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's often useful, to us and you, to post the entire error message, copied and pasted, exactly as it appears on your end. I suspect every one of your errors was complaining about the use of a large 'I' in the method name 'println()' rather than a small 'L'.

Edit: And the statement:

final int x;

Also has issues. See if you can find out why with further reading.
 
Marshal
Posts: 78406
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags, and changed all your text to black, because both those things make it much easier to read. Unfortunately, that also highlights a copying error on your part. The method called is println and you have written printIn throughout. There ought to be an l for lion and you have written an I for ink.
Please go back to the book, and try again, now you know about that error.
And what you are getting as “cannot find symbol …” is not a warning from the compiler, but an error.
 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to the both of ye for your time on this. Yes, it was an error - I'm blaming the font type, silly though.

I've compiled all these again and they work fine, except for the one below. Also this final int x; it's not been initalized yet and declared final but you still can assign a value after this declaration no ? Or am I off point altogther ?

Also, I compiled these two:



//non-static variable x cannot be referenced from a static context - compilation warning

And this...



And got no compilation warning, yet to my eyes this also references a non-static variable from a static context ?
 
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jay sugrue wrote:



And got no compilation warning, yet to my eyes this also references a non-static variable from a static context ?



Hint: take a look again... the static main() method is actually *not* accessing the instance variable.

Henry
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags <- click on to see what code tags are and how to use them. I added them for you, but I look forward to you reviewing the link and using them yourself next time. If you have questions about their use, please ask.
 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Greg will do...Henry, forgive me here I'm pretty fresh to Java but what do you mean by the static method not accessing the instance variable ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jay sugrue wrote:Sorry Greg will do...Henry, forgive me here I'm pretty fresh to Java but what do you mean by the static method not accessing the instance variable ?


You've got two x variables - one instance variable and one method argument. And it's not using the one you think it is.
 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"You've got two x variables - one instance variable and one method argument. And it's not using the one you think it is. "
- I understand that one is an instance variable and the other is a method argument but I'm not sure I know what you mean by the static method not using the instance variable - from public static void...onwards, including everything between the emboldened braces of this method, isn't this all considered a 'static context', ie. including System.out.println(x); and therefore unable to take instance variables ?


 
Henry Wong
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jay sugrue wrote:"You've got two x variables - one instance variable and one method argument. And it's not using the one you think it is. "
- I understand that one is an instance variable and the other is a method argument but I'm not sure I know what you mean by the static method not using the instance variable - from public static void...onwards, including everything between the emboldened braces of this method, isn't this all considered a 'static context', ie. including System.out.println(x); and therefore unable to take instance variables ?




How about if we rename the variable names, so that you can envision what the compiler is seeing....



Does that help?

Henry
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jay sugrue wrote:...but I'm not sure I know what you mean by the static method not using the instance variable - from public static void...onwards, including everything between the emboldened braces of this method, isn't this all considered a 'static context', ie. including System.out.println(x); and therefore unable to take instance variables ?


Static context---consider it as an area will be covered by the Static method and/or Static block between {...} in your program.

 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so Gaurangkumar, from what you say both methods come within the static context, so why then can an instance variable be passed into this static context - is that not against the rules ? Henry, in regards to your reply, how would the compiler know what instance x is referring to ?
 
Marshal
Posts: 27895
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler starts with the smallest context containing the statement, and works its way out to the largest context. At each step it looks for declarations of the variable "x", and when it finds one, it stops looking.

In this case it finds a declaration of "x" in the smallest context, namely the method in which the statement is located. That declaration is the declaration of a parameter named "x". And so it doesn't get around to looking at the declaration of "x" at the class level. So whether that was a static or instance variable is irrelevant to the compiler.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jay sugrue wrote:...why then can an instance variable be passed into this static context - is that not against the rules ?


Q.1). Have you know about "Parameter Passing" in java?
Your main issue: non-static variable x cannot be referenced from a static context.
Q.2). What is the meaning of "referenced"?
If you get the information about Q.1 and Q.2, you will get the idea...
 
Henry Wong
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jay sugrue wrote:Henry, in regards to your reply, how would the compiler know what instance x is referring to ?



For the exact behavior as defined in the Java Language Specification, see ....

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.4.1

Or specifically, this paragraph...

Java Language Specification wrote:A declaration d of a local variable or exception parameter named n shadows, throughout the scope of d, (a) the declarations of any other fields named n that are in scope at the point where d occurs, and (b) the declarations of any other variables named n that are in scope at the point where d occurs but are not declared in the innermost class in which d is declared.



yeah, kinda legalese -- probably easier to just remember your example...

Henry
 
Henry Wong
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurangkumar Khalasi wrote:

jay sugrue wrote:...why then can an instance variable be passed into this static context - is that not against the rules ?


Q.1). Have you know about "Parameter Passing" in java?
Your main issue: non-static variable x cannot be referenced from a static context.
Q.2). What is the meaning of "referenced"?
If you get the information about Q.1 and Q.2, you will get the idea...



Another point... the designers of Java didn't just make up that rule because they felt like it. There is a common sense reason why instance variables can't be accessed from a static context -- the static context doesn't have access to a "this" variable. If you understand the reason, then you can understand how you can get around the issue, either by using another instance besides the "this" instance, or passing the value via another means, such as a parameter. etc.

Henry
 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, and cheers Paul for that, plenty to go on there...will go back and have a closer look at it. Stay tuned, I may need ye again !
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:... There is a common sense reason why instance variables can't be accessed from a static context -- the static context doesn't have access to a "this" variable. If you understand the reason, then you can understand how you can get around the issue, either by using another instance besides the "this" instance, or passing the value via another means, such as a parameter. etc.


As jay sugrue can not distinguish between "pass by value"(E.1) and "pass by reference", i have put my first Q.1 about Parameter Passing for him (So, he will get the idea himself... ).
E.1

E.2

And second Q.2 about "referenced" word from the sentence " non-static variable x cannot be referenced from a static context."(i.e. Compiler Error; Get when try to compile E.2), so that he can able to figure out (if he try to search for it ) that in E.2, the sentence "System.out.println(x);" is equivalent to "System.out.println(this.x);" i.e. x is referenced by the 'this' variable.
But whatever it is...Might he got the answers!!! ... And also thank you for your response...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic