aspose file tools*
The moose likes Beginning Java and the fly likes Compilation problem using static and final modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation problem using static and final modifiers" Watch "Compilation problem using static and final modifiers" New topic
Author

Compilation problem using static and final modifiers

jay sugrue
Greenhorn

Joined: May 29, 2012
Posts: 20
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 !
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: May 29, 2012
Posts: 20
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 ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
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

Joined: May 29, 2012
Posts: 20
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 ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: May 29, 2012
Posts: 20
"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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
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

Joined: May 29, 2012
Posts: 20
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 ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

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

Joined: Jun 02, 2012
Posts: 187
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: May 29, 2012
Posts: 20
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

Joined: Jun 02, 2012
Posts: 187
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.
 
subject: Compilation problem using static and final modifiers