aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessing static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "accessing static method" Watch "accessing static method" New topic
Author

accessing static method

J Recker
Greenhorn

Joined: Aug 26, 2006
Posts: 18
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

Joined: Jul 04, 2006
Posts: 235
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

Joined: Aug 26, 2006
Posts: 18
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

Joined: Aug 26, 2006
Posts: 18
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

Joined: Apr 25, 2005
Posts: 809
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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
J Recker
Greenhorn

Joined: Aug 26, 2006
Posts: 18
Thank you very much for clarifying things.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Charith Fernando
Ranch Hand

Joined: Sep 12, 2005
Posts: 67
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...


Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing static method