Reputation: 115
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.
Upvotes: 0
Views: 6954
Reputation: 1
It appears you are trying to define a StudentDetails
class object that contains rollNumber
and studentName
as instance variables. You are then creating a StudentDetails
object named firststudent
and attempting to pass in the values 19236
and "Thomas"
.
firststudent
is a StudentDetails
object
rollNumber
and studentName
are instance variables.
19236
and "Thomas"
are values.
As instance variables each StudentDetails
object (firststudent
, secondstudent
, ...etc) can have a unique rollNumber
and studentName
.
Now just for an example, if you changed
int rollNumber;
To
static int rollNumber;
It now becomes a class variable and each StudentDetails
object (firststudent
, secondstudent
, ...etc) will share the same rollNumber
. Changing the rollNumber
will change it for all studentDetail
objects.
Upvotes: 0
Reputation: 89
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
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!!
Upvotes: 3
Reputation: 2873
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
Upvotes: 6
Reputation: 12999
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");
Upvotes: 0