1

I am completing a basics programming course and I am having trouble understanding the difference between class variable and instance variables (static and non-static). I need to determine the difference between the two in the code

Class StudentDetails
{
Int rollNumber;
String studentName;
}
StudentDetails firststudent= StudentDetails (19236, ”Thomas”);

I believe the rollNumber, studentName, 19236 and "Thomas" are all instance variables and the firststudent is a class variable.

Any assistance on this would be appreciated as the course material is not very helpful.

RhysBailey21
  • 117
  • 3
  • 13
  • `firststudent` is not a class variable at all because it's not inside a class. – GSerg Jan 23 '20 at 10:41
  • 1
    Does this answer your question? [Static vs Instance Variables: Difference?](https://stackoverflow.com/questions/21204589/static-vs-instance-variables-difference) – Frauke Jan 23 '20 at 11:21

3 Answers3

7

Class variables: only have one copy that is shared by all the different objects of a class,

class StudentDetails
{
  static Int rollNumber;
  /*...*/
}

Instance variable: Every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.

class StudentDetails
{
  Int rollNumber;
  /*...*/
}

Class and Instance variables are both Member variables

SUNIL DHAPPADHULE
  • 2,755
  • 17
  • 32
4

Instance (or NON-Static) Variables

The same instance variable can have as many values as the number of references to that class, so if you have a class like this and you instatiate it several times, then you can change the value of each instance variable (name) per each object instance (foo and bar):

public class Fancyclass
{
   //this is an instance variable
   string name;

   public static void Main(string[] args)
   {
         Fancyclass foo = new Fancyclass();
         Fancyclass bar = new Fancyclass();
         foo.name = "My Name";
         bar.name = "Your Name";

         Console.WriteLine(foo.name);
         Console.WriteLine(bar.name);
   }
}

Output:

My Name
Your Name

Class (or Static) Variables

On the other hand, a class (or static) variable can ONLY be accessed through the qualified namespace, so you are always accessing the same value really (you literally can't access it through a reference/instance/object of the class. It will show an error if you try). You need it's full "address":

public class ClassExample
{
    //class (or static) variable
    static string name;

    public static void Main(string[] args)
    {
         ClassExample foobar = new ClassExample();
         
         //this will show an error
         foobar.name;
         
         //instead you access it this way
         ClassExample.name = "PUFF!!"

         Console.WriteLine(ClassExample.name);
    }

}

Output:

PUFF!!
GUZZ
  • 61
  • 5
0

Everything defined as a field in the class, is a instance associated. In the below class, both rollnumber, studentName are instance associated.

Class StudentDetails
{
  int rollNumber;
  string studentName;
}

StudentDetails student = new StudentDetails();

If you define a static field in the class, then it is associated with the class.

Class StudentDetails
{
  int rollNumber;
  string studentName;
  static int StudentClassNumber = 123
}

Here, static field StudentClassNumber is associated with the class, not with the instance.

If you want to instantiate StudentDetails as you have mentioned, then you need to define a non-default constructor in the class definition.

Class StudentDetails
{
  int rollNumber;
  string studentName;

  public StudentDetails(int rollnumber, string studentName)
  {
      this.rollNumber = rollnumber;
      this.studentName = studentName;

  }

}

StudentDetails firststudent= StudentDetails (19236, "Thomas");
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58