Static Methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Static Methods

  1. #1
    MKV Guest

    Static Methods


    what happens in C#.net when a static method of a non static class is called?
    Does it load entire class into memory? when is static method preferred over
    instance methods?

  2. #2
    James Guest

    Re: Static Methods

    When a static method is called the static constructor is called if it is
    available. For instance if you have a class that contains static methods
    then you can use the static keyword in front of a constructor name within
    that class to create a static constructor. It has to be paremeterless and
    have the same name as the class. You are accessing the actual class rather
    than an instance of that class.

    class MyClass()
    {

    static MyClass() //static constructor
    {
    //do this when a static method is called
    }

    MyClass()
    {
    //ordinary constructor
    }

    }

    --
    James
    "MKV" <vyasmayank@hotmail.com> wrote in message
    news:3cff6b9d$1@10.1.10.29...
    >
    > what happens in C#.net when a static method of a non static class is

    called?
    > Does it load entire class into memory? when is static method preferred

    over
    > instance methods?




  3. #3
    MKV Guest

    Re: Static Methods


    Thanks James for replying about accessing static method and static constructor.
    Other question was about what it loaded in memory when a static method of
    a class is classed? Anyone, please correct if this is wrong: When a static
    method is called, framework loads all member functions and variables of a
    class to memory. It does not initialize all member variables but the loading
    overhead is still there...

    Mayank.

    "James" <James@birdwire.co.uk> wrote:
    >When a static method is called the static constructor is called if it is
    >available. For instance if you have a class that contains static methods
    >then you can use the static keyword in front of a constructor name within
    >that class to create a static constructor. It has to be paremeterless and
    >have the same name as the class. You are accessing the actual class rather
    >than an instance of that class.
    >
    >class MyClass()
    >{
    >
    >static MyClass() //static constructor
    >{
    > //do this when a static method is called
    >}
    >
    >MyClass()
    >{
    > //ordinary constructor
    >}
    >
    >}
    >
    >--
    >James
    >"MKV" <vyasmayank@hotmail.com> wrote in message
    >news:3cff6b9d$1@10.1.10.29...
    >>
    >> what happens in C#.net when a static method of a non static class is

    >called?
    >> Does it load entire class into memory? when is static method preferred

    >over
    >> instance methods?

    >
    >



  4. #4
    Tom Guest

    Re: Static Methods


    "MKV" <vyasmayank@hotmail.com> wrote:
    >
    >Thanks James for replying about accessing static method and static constructor.
    >Other question was about what it loaded in memory when a static method of
    >a class is classed? Anyone, please correct if this is wrong: When a static
    >method is called, framework loads all member functions and variables of

    a
    >class to memory. It does not initialize all member variables but the loading
    >overhead is still there...



    MKV--
    I have not used much in the way of static methods, but I do know that static
    variables are only loaded once. Irregaurdless of how many instances of that
    class exist. I have to believe that a static method works much in the same
    way. (Here is an assumption) a static method (when created) gets loaded
    into a block of memory that stays resident as long as any one instance of
    that class is in scope.

    When a static method is called, all of the member functions and static
    variables are already in memory. That's the way it is regaurdless of whether
    a method is static or not. Suppose you have a class called myClass. The
    moment you type: myClass firstInstance = new myClass();
    myClass gets loaded into memory (this includes all static fields and ALL
    methods). Later when you type firstInstance.MyVariable = 5; that is when
    the Instance Fields get loaded into memory.

    That is my understanding of how it works. It doesn't really matter if a
    method is static or not, when an object is instantiated all methods and functions
    are loaded(along with static fields). The reason you need static methods
    is so that you can update or change static fields.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center