This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing static method

 
J Recker
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having difficulty with the question of the day:




The answer is: compile error. You will get an error saying something like

"Can't make static reference to method int Omb(float)".


You could modify this code to run by including lines in the main method such as

Iaing i=new Iaing();
i.Omb(99);

In which case the class would compile and output 99.0


I ran the above code, and got the same results as they did. However, I do not understand why creating an instance of the class will allow you to access the method from a static method. I thought that you couldn't access an instance method from a static method? Or is that only that you cannot access an instance member from a static method?
Also, when you make the suggested changes, why does the method return 99.0, not 99? Is it because you cast it to an int from a float because the return type is int, so you get the float result?

Help!

 
Surendra Kumar
Ranch Hand
Posts: 236
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance methods can not be accessed directly from a static method. You have to create an instance to access them.
static methods can't have this reference.

Because you're printing a flot number, the integer is converted to float and printed as 99.0
 
J Recker
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this work the same if you were trying to access an instance variable from a static method? You can only access it if you create an instance of the class and access it through the instance?
 
J Recker
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, I modified the code:


Output: 99.0
99
2

Is this the way that you would access an instance variable from a static method?
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this work the same if you were trying to access an instance variable from a static method? You can only access it if you create an instance of the class and access it through the instance?


Yes

instance variables belong to an instance whereas static things belong to a class. So accessing an instance will always require an instance irrespective of from where you are calling it.

If you are calling instance thing from a static context, then you need to call it by someObjRef. From instance method, you can use the existing object by using this keyword.

As static things belong to a class, so by class name you can access them. No instance is required to access them but still you can call them by instance.


Naseem
[ August 26, 2006: Message edited by: Naseem Khan ]
 
J Recker
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for clarifying things.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surendra Nichenametla:

Because you're printing a float number, the integer is converted to float and printed as 99.0


The above is not correct. The integer 99 is converted from int to float as it passed to the method Omb as its local variable float f. It is nothing to do with printing. It is a float inside Omb.
 
Charith Fernando
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

instance methods(non-static) belongs to the object.
static methods belongs to the Class.

trying to access a non-static method from a static method will not compile because at this time the non-static method does not exist. that is the concept behind why we cant call a non-static method from a static method. (think of it when each method gets alive)

for the 2nd question of why it returns 99.0 instead of 99...

actually if you look closely it returns 99

but it prints 99.0 because the program is implicitly casting the int value to a float and then it prints the float and after that it does a explicit cast towards an int and returns it...
 
J Brewer
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trying to access a non-static method from a static method will not compile because at this time the non-static method does not exist. that is the concept behind why we cant call a non-static method from a static method. (think of it when each method gets alive)


actually, this is not completely true. you cannot DIRECTLY access an instance variable or instance method from within a static method, but you can if you first create an instance of the class of the method or variable you'd like to access.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic