• 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

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

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



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




 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!


 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic