• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why static/class variables and instance variables have same name?[Mala Gupta Study guide]

 
Jain Amit
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am preparing for OCA Java 7 and found an interesting point in Mala Gupta's study guide. Following is the code



Error: variable num is already defined in class TestClass

In the above class, num is defined as a static variable as well as an instance variable and compiler is not happy. Since, static variables and instance variables can be independently resolved I am not sure why Java languages restricts this? Anyone can explain from Java design perspective?

Thanks
Amit
 
Richard Hayward
Ranch Hand
Posts: 116
3
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(new TestClass()).num

Would refer to what?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jain Amit wrote: Anyone can explain from Java design perspective?

Duplicate fields and duplicate local variables are compile errors. Change your static int to byte or String and you;d have the same problem.
 
Jain Amit
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Richard,

thanks your example clears the confusion...
thanks...
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jain Amit,

First of all, a warm welcome to CodeRanch!

Jain Amit wrote:In the above class, num is defined as a static variable as well as an instance variable and compiler is not happy. Since, static variables and instance variables can be independently resolved I am not sure why Java languages restricts this? Anyone can explain from Java design perspective?

Static/class variables and instance variables can not be independently resolved, because you can access a static/class variable from any reference variable of that class as well; even a null reference as this code snippet illustrates
And just for completeness: local variables and method parameters can have the same name as static/class and/or instance variables, because you can differentiate between these variables: using the class name for static/class variables (or this), this for instance variables and just the variable name for local variables and method parameters). Illustrated in this code snippet:

Hope it helps!
Kind regards,
Roel
 
Liutauras Vilda
Bartender
Pie
Posts: 2812
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Roel's example (just a bit modified):
If method parameter or local variable has the same name as an instance variable - then parameter or local variable has higher precedence than instance variable.
 
Jain Amit
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel,

Thanks for the warm welcome. It's crystal clear now.

My OCA Jave SE 7 I is scheduled for this month end. Will share the test results soon.

Thanks
Amit
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jain Amit wrote:My OCA Jave SE 7 I is scheduled for this month end. Will share the test results soon.

Best of luck!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic