2

I have a test later today, and I think I may be stuck on this part of the study guide:

Classes and objects, references, methods; the class as a pattern for creating objects, the concept of a class and object as defining a unit with data members and methods; what is an instance of a class; static members of a class; what it means for a member of class to be public

My best effort to explain these concepts is, as follows:

A class is a programmer defined data type that is composed of data members and methods.

An object is an entity consisting of values (characteristics and traits) and methods (capabilities or behaviors). A class is like a blueprint from which objects are created.

A reference variable points to an object created in another memory location. (not 100% sure what this means)

Much like a cookie cutter can be used to create individual cookies, a class can be used to create individual objects, or instances of that class.

A static member of a class (or class variable or method) belongs to the class and is not owned by any object of the class.

If a member of a class is public, that means that it can be accessed by other parts of the program.

I'm wondering if this is going to be satisfactory or if I'm missing anything critical. Obviously I'm shaky on the idea of reference and reference variables, and I think I'm still trying to conceptualize objects and classes in a way that's sufficiently explainable.

Thanks for your help, in advance.

4

5 回答 5

3

A class is a template from which objects can be built; it acts as a blueprint for creating objects.
An object is an instance of a class.

Much like how an architect draws a blueprint of a house. The blueprint and constructed house are two different things. From that one blueprint, the same house can be built in a lot of places. Similarly, you can create multiple objects of a class.

The blueprint defines how a house is supposed to look like. There is going to be a kitchen, a couple of bedrooms, a basement (possibly for hiding illegal money), etc. Each person who buys a house will customize the house in a different way.
This is akin to values in an object - same variables in different objects may have different values.
Get it? Same bedroom in two different houses constructed from the same blueprint will have different color, bed, lighting, etc. Same thing, different value.

Here is how you understand static and instance variables. The kitchen belongs to everyone - it is static and is shared by everyone and only one kitchen exists (static). Your guitar belongs to you and you only, it is not shared with anyone but it may happen that you sister also has a guitar that belongs to her and her only.
What I am saying is - every object has its own copy of instance variables while there is only one copy of static variables that is shared by all the objects of that class.

Here is how to understand changing the values of static and instance variables
If you were to get custom graphics for your guitars, it will matter to you and you only because it is an instance variable. If there was some change made to the kitchen, say the fridge was moved, it affects everyone because kitchen is a static variable.
What I am saying is - changes to instance variables are only visible to the object that owns them while changes to static variables are visible to all the objects of that class.

于 2014-07-02T18:05:25.123 回答
2

Seems pretty spot-on to me. If I'm being pedantic, I'd recommend changing the last two sentences:

A static member of a class belongs to the class and is not owned by any instance of the class.

If a member of a class is public, that means that it can be accessed by other classes within the program.

To understand references, you also might want to read this: http://www.javaranch.com/campfire/StoryCups.jsp

And its follow-up: http://www.javaranch.com/campfire/StoryPassBy.jsp

于 2014-07-02T18:07:12.607 回答
1

You seem to have everything else nailed down pretty pat. For newbies, reference types and reference variables are continuous points of contention because they have a tendency to get confused with something else.

The easiest way to understand reference types is to compare it in terms of houses. Your house is the value, your street address is a pointer to your house (a reference), and your full address -- could -- be considered to be your reference variable when used on an envelope.

See what I did there?

于 2014-07-02T18:06:27.647 回答
1

For the most part, you seem to have the material down. I'm going to try and answer your implied question here,

A reference variable points to an object created in another memory location. (not 100% sure what this means)

A reference (in Java) is a bit like a po box number. You can go to the post office and find lots of po boxes, but the number makes the specific box easy to find. Also, it is unique within the post office. It ensures the mail is kept separate from the other boxes. The analogy is not perfect (note the key is largely immaterial to this discussion).

于 2014-07-02T18:09:14.020 回答
1

Yes you are spot on with classes and how objects are instances of them. A class is like a template that contains all the attributes/characteristics. Within a class, there can be objects, which are more of instances of the class. For an example, say there's a class called 'Animal'. That means whatever items are in the class all have one thing in common: they're animals. Now, an object of the class 'Animal' would be a specific animal (bird, elephant, giraffe, dog). These would be objects under the class 'Animal' because they're all animals, but they're objects because they are instances of animals (kinds of animals). Also, when a member/method of a class is public, it can be accessed by other parts of the program or project. Hope this helps.

于 2015-09-30T17:26:37.283 回答