Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the difference between member functions and static member functions?

user-image
Question ajoutée par Marsed Elamin Elmamoun Ahmed , Senior IT Engineer (Part time) , KHADARNA CO.LTD
Date de publication: 2013/10/13
Manoj Sankar
par Manoj Sankar , Java Developer , Core Mind Technologies

Member Function:

 In order to access the member Function we need to create a object for that function.

Ex:

class A

{

void display()

{

System.out.println("Member Function");

}

public static void main()

{

A a=new A();

a.display();

}

}

Static Member Function:

In order to access ststic member function we don't need object to call the function.

Ex:

public class A{    static void display()    {        System.out.println("Static Member");            }public static void main(String args[]){    A.display();//Accessing the Static Member Function    System.out.println("hello Java");}}

Amen Ayach
par Amen Ayach , .Net Team Leader, Project Coordinator and Senior Solution Designer , Netways

Static functions could be called without initiating an instance of containing class, for example :

//========================================

public Class Test{

    public Test(){}

   

    public func1(){

         //do something

    }

 

    public static func2(){

         //do something

    }

}

//=======================================

So to call func2 you can do it as following:

Test.func2();

 

But to call func1, you should do the following:

Test mTest = new Test();

mTest.func1();

 

_________________________________________

NB:: if you use static function be aware to clean you code, because static stuff could live in the pc memory after the function finished it job.

Gayasuddin Mohammed
par Gayasuddin Mohammed , Advocate , Practicing Law before High Court at Hyderabad

static member functions are referenced with the class and available for all objects of the class and in general with the class reference, whereas the member functions are referenced with objects created to the class and available to that objects.

Eslam Gamal
par Eslam Gamal , MIS Executive - Credit Risk , QIB

Static Function and classes are similar to modules in Classic Visual basic (VB6) , you dont have to instantiate the class to call the methods / variables properties , the static class can be accessed from anywhere if specified (Public static , private static , etc...)

 

Non-static normal class / method : you must create instance to access the code , hint ( oop : Encapsulation) 

Cedric Nabaa
par Cedric Nabaa , Java developer , Capital Banking Solutions

when you use static you do not need an instance of your class to access the field/function where as when you do not define your function/field as static you have to instanciate your object

Khalid Omar
par Khalid Omar , Senior .NET Developer / Associate Technology Manager , Musafir.com (Universal Tourism)

The answer is similar for all languages. A static fuction belongs to the class definition and is accisible with and without having an instance of the class

 

A member function belongs to an instance and can be only accessed via a class instance:

 

To spice things up a little, he is a funnier answer:

A member function requires an extra hidden parameter (which the compiles hides from you, but believe me it is there),. This extra hidden parameter is a pointer to a class instance to be used by the function to operate on. That is actually the this keyword you use to acces the current object...

 

A static member function does not have any hidden parameters and therefore it cannot access any instance-specific members of a class that are not static

 

However, both member and static functions of a class have access to private and protected members of a class (static methods need an explicit class-instance-pointer parameter to access non static properties)

All static methods in a class are class members, as all static members get loaded into the memory while class loading and these are not available inside an object.

As we trigger JAVA command class loads into the memory i.e.; all static members including functions, global variables, after that main method is executed.

 

while all non static members of a class are members of an object i.e; all can be accessed through object  creation and no static member of a class is available inside an object.

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?