none
Static vs non static newbie struggle.

    Question

  • This is my code:

    using System;
    using System.Windows.Forms;
     
    namespace NonStaticTest
    {
        public partial class Form1 : Form
        {
            public MyClass helloWorld;
     
            public Form1()
            {
                InitializeComponent();
                helloWorld = new MyClass();
            }
     
     
            public class MyClass
            {
     
            }
     
     
        }
    }
    

    Whats the difference of making reference like this:

    public MyClass helloWorld;

    Or this:

    public static MyClass helloWorld;

    Thursday, April 13, 2017 2:49 PM

Answers

  • If you declare a field or another member as static it belongs to the type rather than to an instance of the type.

    So if you create an instance of your form class like this:

    Form1 frm1 = new Form1();


    ...you can only access the public field of that particular instance if you define the field as non-static:

    var hw = frm1.helloWorld;

    In this case each instance of Form1 that you create gets its own copy of MyClass.

    If you define the field as static, there is only one MyClass for all instances of Form1 and you access it using the type name:

    var hw = Form1.helloWorld; //"Form1" is the name of class and not the name of an instance of the class

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Ryan Software Friday, April 14, 2017 5:45 PM
    • Marked as answer by 1337Logan Saturday, April 15, 2017 7:50 PM
    Thursday, April 13, 2017 6:57 PM
  • Your first statement creates an address for an object Helloworld which can be constructed one or more times and is then released when it goes out of scope (handled by the Garbage Collector on a proper time).

    Your second statement creates an address for the object Helloworld and construct that object new fixed direct on the Main part (assembly) of your program. It will stay in memory as long as the program runs. It is therefore not OOP but more called modular programming (be aware it is still using  OO design). 

     

    Success
    Cor



    • Edited by Cor Ligthert Thursday, April 13, 2017 3:19 PM
    • Marked as answer by 1337Logan Saturday, April 15, 2017 7:51 PM
    Thursday, April 13, 2017 3:13 PM

All replies

  • Your first statement creates an address for an object Helloworld which can be constructed one or more times and is then released when it goes out of scope (handled by the Garbage Collector on a proper time).

    Your second statement creates an address for the object Helloworld and construct that object new fixed direct on the Main part (assembly) of your program. It will stay in memory as long as the program runs. It is therefore not OOP but more called modular programming (be aware it is still using  OO design). 

     

    Success
    Cor



    • Edited by Cor Ligthert Thursday, April 13, 2017 3:19 PM
    • Marked as answer by 1337Logan Saturday, April 15, 2017 7:51 PM
    Thursday, April 13, 2017 3:13 PM
  • static class is basically the same as a non-static class, except that a static class cannot be instantiated. 

    Specifically, if you look at the IL code, a static class will be abstract and sealed which denotes two important qualities:

    - It cannot be inherited
    - You cannot create instances from it

    A static class is a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields.


    william xifaras




    Thursday, April 13, 2017 6:36 PM
  • If you declare a field or another member as static it belongs to the type rather than to an instance of the type.

    So if you create an instance of your form class like this:

    Form1 frm1 = new Form1();


    ...you can only access the public field of that particular instance if you define the field as non-static:

    var hw = frm1.helloWorld;

    In this case each instance of Form1 that you create gets its own copy of MyClass.

    If you define the field as static, there is only one MyClass for all instances of Form1 and you access it using the type name:

    var hw = Form1.helloWorld; //"Form1" is the name of class and not the name of an instance of the class

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Ryan Software Friday, April 14, 2017 5:45 PM
    • Marked as answer by 1337Logan Saturday, April 15, 2017 7:50 PM
    Thursday, April 13, 2017 6:57 PM