• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static and Final  RSS feed

 
C Jay
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am reading the topic static and final in "Head First Java" book and there are these questions (the ones I did not get right) to find if the following code will compile or not:

Question 1:



My thought process: go is a non-static method (it can print the static variable x = 12, that should be okay) but wait..it takes a final int x as its argument (okay, now this x variable is local to the go method, I should be careful in not changing this local variable inside this method since it is marked "final"), tries to print variable x (come on, now is this the local variable or static variable, hmm..compiler should be confused...)..so my answer was Compilation error..

I tried the code but the code ran fine {An instance f for Foo5 and f.go(5) prints 5}.

Question 2:



My thought process for this question: Instance variable x is 12. There is a static method go (so this gets initialized at Class initiation time and not per instance) and since the rule is static method can access only static variables, x is not known at Class initiation time ..so (this time at least) is it Compilation error?

Tried the code, wrong again. No compilation error {with f is an instance of Foo6, f.go(5) prints 5}.

Could someone please clarify these for me? Where am I going wrong or what am I missing?
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removed my answer because I was party wrong and a much clearer answer was given below.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C Jay wrote:Could someone please clarify these for me? Where am I going wrong or what am I missing?

In both cases the x used in line 4 is the x parameter passed to the go method. In neither case is the x declared and initialised on line 2 used anywhere else in the code.
In the first case you could use the x declared on line 2 in the go method by prefixing it with the class name

In the second case you could use the x declared on line 2 in the go method by prefixing it with this

Edit: Except, as pointed out below, the go method is static in the second example, so this won't work. You'd have to create an instance of Foo6 to access the instance variable
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both bits of code will both compile and run with the same output.
The difference is that one has a static method, so you ought to call it as Foo6.go(123); and the other has an instance method which must be called on an object reference name, e.g. new Foo5().go(234);

You are nearly there, realising that the parameter behaves rather like a local variable. Read about shadowing, but that Java Language Specification link is difficult to understand.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart. In the second case you couldn't access the instance field because the method is declared static.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ole Kristian Sandum wrote:Stuart. In the second case you couldn't access the instance field because the method is declared static.

You're right. I missed that.
 
C Jay
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. Especially with Stuart's detailed explanation and Campbell's link on "shadowing" concept, I finally got my doubt cleared.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slight aside; this thread is headed “static and final”, but the actual problem was something else. The bits about static and final may be in the puzzle as red herrings.
 
C Jay
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you think this discussion should be moved to a different forum, kindly do so. Thanks.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is probably the best forum for this question at the moment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!