• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Accessability of static methods.

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear freinds,
I am preparing for SCJP 6.
As we all know Static methods don't know about instance variables and methods of a class.but at one place in K&B i have seen snippet shown below

a is the reference variable for the concerned class myMethod() is the static method defined in the class.I am unable to get the reason given below.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code showing the class and method which is called ,it's not possible to predict the behaviour of this code without that.
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit, you can call static methods using reference of a class. Its right that static methods don't have direct access to instance fields/methods of the class. But you can still call static methods using reference of a class. Writing a.myMethod() is equivalent to writing MyClass.myMethod() (in this case)...
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:Ankit, you can call static methods using reference of a class.



For a minute there I was scratching my head thinking 'Err... why is Ankit talking to himself'.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Deepak Bala wrote:
For a minute there I was scratching my head thinking 'Err... why is Ankit talking to himself'.


 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Deepak Bala wrote:
For a minute there I was scratching my head thinking 'Err... why is Ankit talking to himself'.




 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, to revisit the original question, this is a situation where, by using ancillary
information, the java compiler can confuse a new programmer. It is definitely true
that static methods cannot operate on objects. From this perspective, for a static
method, the code presented above is definitely wrong. However, this syntax is
allowed because the compiler does not use the object. Rather, it uses the type
of the reference variable, as demonstrated below. Jim ... ...
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do:


It's like you were doing:


=D
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jim Hoglund wrote:Okay, to revisit the original question, this is a situation where, by using ancillary
information, the java compiler can confuse a new programmer. It is definitely true
that static methods cannot operate on objects. From this perspective, for a static
method, the code presented above is definitely wrong. However, this syntax is
allowed because the compiler does not use the object. Rather, it uses the type
of the reference variable, as demonstrated below. Jim ... ...



Yes the concept demonstrated above is a slithery snake indeed. But hey you caught and that is another feather in your cap
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanking you all for "to the point" response.
Having pleasure with the preparation of SCJP 6
 
Grow a forest with seedballs and this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!