• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why final variables can be redefined in methods and inner class of a class?

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code above is totally fine(although it's meaningless to do so) and the output is 151 154. It makes me surprised that a final variable can be redefined in the method of class with the same name.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner classes are not on the OCA exams!

nemo zou wrote:It makes me surprised that a final variable can be redefined in the method of class with the same name.

Because of "variable hiding".

The parameter of the Changeit method just happens to have the same name (FinalV) as the class variable FinalV of the FinalValue class. And you can access both in the Changeit method:

And as a final note: remember the Java naming conditions for methods and variables, you should use camelCase notation (so finalValue and changeIt). And classes use CamelCase notation (e.g. InnerClass)
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Because of "variable hiding".


Thanks for this~
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember you have "method hiding" as well. Does this code snippet compiles? And if it does, what's the output?
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And remember you have "method hiding" as well. Does this code snippet compiles? And if it does, what's the output?


why does t1.exec(3) is 3 instead of 6 and t3.exec(5) not throw a nullpointerexception?
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t1.exec(3) is 3 is because exec is a static method, I can understand this point . But why does a null object also has static methods/values with it?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:t1.exec(3) is 3 is because exec is a static method, I can understand this point .

Spot-on! The exec method is static, so the type of the reference variable determines which method to execute. In this case the type of t1 is Times, so the Times.exec() method will be executed.

nemo zou wrote:But why does a null object also has static methods/values with it?

For exactly the same reason. To invoke a static method, the compiler is only interested in the type of the reference variable not in the actual object. The type of t3 is Times2, so the Times2.exec() method will be executed. Don't forget: you don't require any object to invoke/access a static method/variable. On the job it's a best practice to always use the class name to invoke/access a static method/variable (instead of a class instance). But on the exam, you might see code like this trying to trick/fool you

You could even write something like thisRemember: this only works with static/class methods, if exec was an instance method, you'll get a NullPointerException (obviously).

Hope it helps!
Kind regards,
Roel
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
You could even write something like thisRemember: this only works with static/class methods, if exec was an instance method, you'll get a NullPointerException (obviously).
l


Another thing: I thought the following code should print out "This is Times3". But the reality is that it does not print anything out...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:Another thing: I thought the following code should print out "This is Times3". But the reality is that it does not print anything out...

That's because your code only requires to load the TestTimes class in memory. Once you invoke the exec method on the reference variable (or the class name), classes Times and Times3 will be loaded in memory and the println statement will be executed.
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nemo zou wrote:Another thing: I thought the following code should print out "This is Times3". But the reality is that it does not print anything out...

That's because your code only requires to load the TestTimes class in memory. Once you invoke the exec method on the reference variable (or the class name), classes Times and Times3 will be loaded in memory and the println statement will be executed.


Understand! Like this code would show

This is Times1
This is Times3
30


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:Understand! Like this code would show

This is Times1
This is Times3
30

Yes, indeed!
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nemo zou wrote:Understand! Like this code would show

This is Times1
This is Times3
30

Yes, indeed!


Thank you for your examples!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic