• 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

instance on static method

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 4
class F {
static String m(float f) {return "f";}
static String m(Float f) {return "F";}
public static void main (String[] args) {
Float f1 = new Float(1);
System.out.print(m(f1.parseFloat("1")));
System.out.print(m(f1.floatValue()));
System.out.print(m(f1.valueOf("1")));
}}
i got this question from dan's
my problem is this:
that program compiles, but one thing eludes me , (parseXXX) and (value of) are both static wrapper method how can they be called using an instance of the wrapper class ?
or am i getting it all wrong?
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can call a static method with an instance
of the corresponding class, I've read this
in K&B as well, but I don't have the book with
me right now to tell you the page number.
Cheers,
Gian Franco
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use an object reference to execute a static method. However, that will access the one class method, not an instance method of the individual class. There is still only one static parseXXX method loaded when the wrapper class is loaded.

Output is: true
MP
 
luc ndabaneze
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i hate to disagree but in kathy sierra's book page 101 it says " the rule is , a static method of a class can't access a non-static(instance) menber method or variable- of it's own class " (sic)
so , i think that the reason it compile it because the compiler is only interested to know wheter the reference is of the class type , but a run time error should be thrown...
what do you think ?
 
luc ndabaneze
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry mark
i was replying to Casoula, i guess you are right
thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this out.
 
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i hate to disagree but in kathy sierra's book page 101 it says " the rule is , a static method of a class can't access a non-static(instance) menber method or variable- of it's own class " (sic)


Luc ,I don't see how this should prohibit instances of any class from calling the class' static methods.
jeff mutonho
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luc,
I'm back home and got my book...

Originally posted by luc ndabaneze:
hi
i hate to disagree but in kathy sierra's book page 101 it says " the rule is , a static method of a class can't access a non-static(instance) menber method or variable- of it's own class " (sic)
so , i think that the reason it compile it because the compiler is only interested to know wheter the reference is of the class type , but a run time error should be thrown...


That's not what I meant. Please look at page 103.
It says:

...But just to make it really confusing, the Java language also allows you to use an object reference variable to access a static member:
Frog f = new Frog();
int frogs = f.getFrogCount(); // Access static method getFrogCount using f
In the preceding code, we instantiate a Frog, assign the new Frog object to the reference variable f, and then use the f reference to invoke a static method! But even though we are using a specific Frog instance to access the static method, the rules haven�t changed. This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member. In the Frog example, the compiler knows that the reference variable f is of type Frog, and so the Frog class static method is run with no awareness or concern for the Frog instance at the other end of the f reference.


Cheers,
Gian Franco
[ April 29, 2004: Message edited by: Gian Franco Casula ]
 
luc ndabaneze
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes u're right franco , corey's tiplines are even better explanation
thanks guys
 
reply
    Bookmark Topic Watch Topic
  • New Topic