ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Marsed Elamin Elmamoun Ahmed , Senior IT Engineer (Part time) , KHADARNA CO.LTD
تاريخ النشر: 2013/10/13
Manoj Sankar
من قبل 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
من قبل 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
من قبل 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
من قبل 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
من قبل 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
من قبل 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)

Akshay Misra
من قبل Akshay Misra

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.

المزيد من الأسئلة المماثلة