Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

wrapper??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


it is giving me compiler error ..its fine coz the method floatValue() should be invoked on an instance of type Float.....


here parseFloat ,valueOf are static and should use their classname ...it is running fine.....

why ???
[ October 23, 2005: Message edited by: srikanth reddy ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by srikanth reddy:


it is giving me compiler error ..its fine coz the method floatValue() should be invoked on an instance of type Float.....


here parseFloat ,valueOf are static and should use their classname ...it is running fine.....

why ???



I can't see any difference between the two versions!!!
What exactly is your question?
is it "why are parseFloat, valueOf working even though they're static methods?"
well, static methods can be invoked directly using class name and also on instances of that class. This is a feature that was added in 1.4 (I guess) because there are some earlier versions that didn't allow this (i.e. the method should be called directly using the classname), but 1.4 and newer versions allow this.
And also make it a point to see which version you are using, and the version in which the book you're reading was written.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kiran now the post was edited ...

yah the doubt i was having is that ..can the static methods be called by the instances of the class...
like valueOf() and parseInt() which are static methods be called using the instances of the class......
..
and i think the reverse is not true that is instance methods (i.e xxxValue()) cant be called using the class name ...as compilation error given for the first code.......


right ........
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can the static methods be called by the instances of the class...


The compiler is not using the instance to invoke the static method; it uses the reference variable. The difference is that the variable can be null and the static method still gets called:



i think the reverse is not true that is instance methods (i.e xxxValue()) cant be called using the class name ...as compilation error given for the first code.......


right ........



Yes, right.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is very simple logic srikanth

A reference variable is an "implicit argument" to a method.
Tell me, is there any use of passing an argument which will not be used?
No, right? thats just the rationale of static or factory methods- they don't use any implicit arguments, so why call them using references? it is allowed of course.
now consider the non-static methods
here we need the implicit arguments to perform certain operation, which is why we need them to be called on references, and it makes no sense to call them on classes

was that clear? please tell us if you require greater detail.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Firstly, "FLOAT.parseFloat("1")" does not work on my compiler, javac 1.4.2, because the class is "Float" (case sensitivity).
  • Secondly, you cannot reference "floatValue()" from a static context."floatValue()" returns the float-type value of the Float wrapper class instance. The value of "f1" is 1.0, and "f1.floatValue()" returns (float) 1.0. You have to appreciate that "Float.floatValue" doesn't make sense.

  • Replace "m(Float.floatValue())" with "m(new Float(1).floatValue())" and consider the difference.
     
    Poop goes in a willow feeder. Wipe with this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic