Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why can't a function be called from a static context 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 "why can Watch "why can New topic
Author

why can't a function be called from a static context

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



SCJP1.6 JAVA 6 CERTIFICATION Guide-310-065
by Katherine Sierra (Author), Bert Bates (Author)





OCPJP 6.0 93%
OCPJWCD 5.0 98%
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

A static method can be called from any method i.e. the static as well as instance method can call any static method or static variable
A static method cannot call instance method without the help of the object

this is because
instance method are related to the instance and not the class
static method are totally related to the class and they have nothing to do with instance created
static methods and variables are the shared members for all the objects in the class

here I am providing a small program that will clear all your doubts regarding the static and non static members of the class


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

As far as i know

the static methods can be regarded as methods that are outside the class (something similar to main() of c/c++) even though they are inside a class.
so we can not access instance members in the static contexts.

they can be regarded as global methods of class & the instance methods are as local...

---> it is possible to access global methods(static) from local(instance) but oppisite is not true....

for example



infact we made the main(string a[]) static for this purpose , imagine if it is not static - it becomes local to the class only & the JVM can not start the program.




Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Suppose hypothetically that "It is allowed by JAVA designers" to call non-static methods from static context...then lets see what would happen...but before that make a note about the following point.

1. Basically non-static Methods are used to access the state (Instance variables) of any object. They act according to the state of the object.


Now suppose, you call the non-static method from the static context......the non-static method would demand for certain instance varibales (read above point 1), but instance variables are yet not created , because instance variables are created with the creation of objects. Next question arises in mind is that , what if , we create certain objects and then call method from the static context , then in this case how would a method or JVM comes to know that whose instance variable are you talking about. No object is under consideration of the static method call.

In fact , the methods whose behavior is same regardless of the state of the objects are marked static.

In a nutshell , remember the following points

1) Members means Methods and Instance variables.
2) Non-static Methods can use non-static members. (Ofcourse)
3) Non-static Methods can use static members.
4) Static Methods CANNOT use non-static members.
5) Static Methods can use only static members.

Thanks !!!




SCJP 6.0 96%

(Connecting the Dots ....)
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Moguluri Ravi Kiran wrote

the static methods can be regarded as methods that are outside the class (something similar to main() of c/c++) even though they are inside a class.
so we can not access instance members in the static contexts.


I think you are confusing the issue here.

Static method belongs to a class.
 
Consider Paul's rocket mass heater.
 
subject: why can't a function be called from a static context