简体   繁体   中英

Need help identifying class/primitive instance variables in this example

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:

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".

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