• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessability of static methods.

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanking you all for "to the point" response.
Having pleasure with the preparation of SCJP 6
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic