0

I'm having a huge issue with what should be a simple question:

Identify primitive instance variables and class variables from below code

class StudentDetails
{
    Int rollnumber;
    String studentname;
}

StudentDetails firststudent = new StudentDetails(19236, "Thomas");

My original submission was sent back to me because I described what primitive instance variables and class variables were and I think I identified them incorrectly. The comments returned state "clearly state which variables are primitive instance variables (there is 1) and which are class variables (there is 1)."

Now I must have read 10 different articles/posts explaining what instance variables and class variables are and I'm still confused in regards to the class variable in question.

In all the examples I've seen, class variables are declared with the static keyword in a class, but outside a method, constructor or a block. In this question there is no static keyword. Is it implied somehow? Is it not required for class variables?

I assume the string studentname is the class variable simply because the int rollnumber is the primitive variable. However, I'm not certain and want to understand the answer.

My concepts for identifying class variables are comes from sites such as:

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Sam
  • 37
  • 1
  • 6
  • Your links are for Java, but you've tagged the question C#. Which language is the one you're using? – Joe Sewell Apr 16 '20 at 16:09
  • Seems like a duplicate question to me. If Java: [In Java, Why String is non-primitive data type?](https://stackoverflow.com/questions/27510494/in-java-why-string-is-non-primitive-data-type). If C#: [In C#, why is String a reference type that behaves like a value type?](https://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type) – Sean Skelly Apr 16 '20 at 17:05
  • Does this answer your question? [In C#, why is String a reference type that behaves like a value type?](https://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type) – Sean Skelly Apr 16 '20 at 17:06
  • Thanks everyone. Yes my assignment was in c#, I've previously had a Java unit and I guess I was looking for the answer across languages but I think that was a mistake as intricacy may arise with how languages work. – Sam Apr 17 '20 at 04:05

1 Answers1

1

This sounds like a terminology issue.

I would agree that a field declared with static in Java or C# is a "class field" (there is only one field, belonging to the class itself) and other fields are "instance fields" (there is a field for each instance of the class).

But it sounds like the question is really asking about "class types" vs. "primitive types". Your code isn't valid Java or C#, but assuming based on context that it's supposed to be Java, int (lowercase) is a primitive type, and String is a class type.

So rollnumber is an instance field, whose value is of a primitive type (int) - thus, it is a "primitive-type instance field". And studentname is also an instance field, but its value is of a class type (String) - thus, it is a "class-type instance field".

I'd recommend discussing this with whomever gave you the assignment, so you understand the terminology they expect in assignments going forward. I imagine they might be using, for example, the term "static field" instead of "class field".

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
  • Thank you for you time Joe. That cleared things up, I think I was possibly over thinking things. I think it was meant exactly as you said with String being class type. My course is online and I've found the help and feedback very limited so I'm very grateful for this website and your time. – Sam Apr 17 '20 at 04:11