简体   繁体   中英

Public variable inside a class

I am a bit confused about how public variables work inside a class. I know that public variables can be accessed without calling the class, whereas private ones cannot.

If you have multiple instances of the same class and in each you give a different value to a public variable then i assume each class instance would have its own unique version of the variable each with a different value.

My confusion is this

What happens if you change the value of the public variable without calling a new instance of the class?

Would all future new instances of the class start with that variable set to whatever you set it to without first calling the class? if not then what happens?

I think you somehow mixed up public variables and static variables. The below statement:

public variables can be accessed without calling the class

Is utterly and plainly wrong for non static variables, might they be private or public.

If you change public variable of a class instance, it will change only that instance , having no effect whatsoever on other existing instances or future instances.

Static variables on the other hand can indeed be called without an instance of the class and changing them has "global" effect, not related to class instance.

(If you have any specific code you worked with and need further guidance please post it)

In languages like C#, you can´t do that.
Accessing any (non-static) variable of a class itself instead of a instance of it
would result in a compiler error.

Perhaps some sample code will help; private variables cannot be accessed so I'm guessing your terminology is getting mixed up. In the below sample, the PublicStaticInt property is declared as static; simply put, that means there is only one copy of it in your AppDomain. Every instance of ExampleClass that is created references that same copy of it. The PublicInt property is an instance property; that means that every instance of ExampleClass has its own copy of that piece of data.

    public class ExampleClass
    {
        public static int PublicStaticInt { get; set; }

        public int PublicInt { get; set; }
    }


    private static void Main(string[] args)
    {
        for (int i = 0; i < 5; i++)
        {
            var example = new ExampleClass();
            Console.WriteLine("PublicStaticInt = " + ExampleClass.PublicStaticInt.ToString() + ", PublicInt = " + example.PublicInt);
            ExampleClass.PublicStaticInt++;
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

When you create a new instance of same class then every instance has its own values for each data members. for example

    class A
        {
         public int a;
         public int b;
           public A()
             {
            a=0;
            b=0;
             }
          }

when i create instance of class A like this

A a11 =new A();
A b11 = new A();

Now if I change the value of a11 it will never change the value of b11 and if I change the value of b11 it will never change the value of a11.

Would all future new instances of the class start with that variable set to whatever you set it to without first calling the class? if not then what happens?

No they will set to values that you have set in the constructor of the class. In this case it will set values of a and b equal to zero for every instance of class because it is done the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM