All of the variables provided by Java other than the eight primitive variables mentioned above are reference type. A programmer is also free to create their own variable types by defining new classes.
In practice, any object instanced from a class is a reference variable. Let's look at the example from the beginning of the chapter where we created a variable called 'leevi' of type Name. The most significant difference between primitive and reference variables is that primitives usually numbers are immutable. The internal state of reference variables, on the other hand, can typically be mutated. This has to do with the fact that the value of a primitive variable is stored directly in the variable, whereas the value of a reference variable is a reference to the variable's data, i.
Arithmetic operations, such as addition, subtraction, and multiplication can be used with primitive variables — these operations do not change the original values of the variables.
Arithmetic operations create new values that can be stored in variables as needed. Conversely, the values of reference variables cannot be changed by these arithmetic expressions. The value of a reference variable — i. Let's assume that we have a Person class available to us, containing an instance variable 'age'. If we've instantiated a person object from the class, we can get our hands on the age variable by following the object's reference. The value of this age variable can then be changed as needed.
We mentioned earlier that the value of a primitive variable is directly stored in the variable, whereas the value of a reference variable holds a reference to an object. We also mentioned that assigning a value with the equality sign copies the value possibly of some variable on the right-hand side and stores it as the value of the left-hand side variable. A similar kind of copying occurs during a method call.
Regardless of whether the variable is primitive or reference type, the value passed to the method as an argument is copied for the called method to use. With primitive variables, the value of the variable is conveyed to the method. With reference variables, it's a reference. Let's look at this in practice and assume that we have the following Person class available to us. The program's execution starts off from the first line of the main method.
A variable of type Person is declared on its first line, and the value returned by the Person class constructor is copied as its value. The constructor creates an object whose birth year is set to and whose name is set to the value received as a parameter. The constructor returns a reference. Once the row has been executed, the program's state is the following — a Person object has been created in memory and the first variable defined in the main method contains a reference to it.
The size affects the bark. You can send things to a method. You can get things back from a method. Note The bits representing 42 are returned from the giveSecret method, and land in the variable named theSecret.
You can send more than one thing to a method. Calling a two-parameter method, and sending it two arguments. You can pass variables into a method, as long as the variable type matches the parameter type.
Java is pass-by- value. That means pass-by- copy. There are no Dumb Questions Q: Q: What happens if the argument you want to pass is an object instead of a primitive? Q: Q: Can a method declare multiple return values? A: A: Sort of. Q: Q: Do I have to return the exact type I declared?
A: A: You can return anything that can be implicitly promoted to that type. Q: Q: Do I have to do something with the return value of a method? Bullet Points Classes define what an object knows and what an object does. Things an object does are its methods behavior.
Methods can use instance variables so that objects of the same type can behave differently. A method can have parameters, which means you can pass one or more values in to the method. Cool things you can do with parameters and return types. Do it or risk humiliation and ridicule. Note By forcing everybody to call a setter method, we can protect the cat from unacceptable size changes.
Hide the data. Note Mark instance variables private. Mark getters and setters public. HeadFirst: Can you give me an example? Encapsulating the GoodDog class. Note Any place where a particular value can be used, a method call that returns that type can be used.
How do objects in an array behave? Declare and create a Dog array, to hold 7 Dog references. Declaring and initializing instance variables. Note Instance variables always get a default value. The difference between instance and local variables. Comparing variables primitives or references. Make it Stick. Given the method below, which of the method calls listed on the right are legal? BE the compiler. Who am I? A class can have any number of these. Mixed Messages A short Java program is listed to your right.
Pool Puzzle. Five-Minute Mystery. Q: Do I have to return the exact type I declared? A method can have only one of these. This can be implicitly promoted. I prefer my instance variables private. Only setters should update these. A method can have many of these. I return something by definition. We say in this case that std holds a null pointer or null reference. The null pointer is written in Java as " null ". You can store a null reference in the variable std by saying.
It is not correct to say that the variable "points to null"; in fact, the variable is null. For example, you can test whether the value of std is null by testing. If the value of a variable is null , then it is, of course, illegal to refer to instance variables or instance methods through that variable—since there is no object, and hence no instance variables to refer to! For example, if the value of the variable std is null , then it would be illegal to refer to std.
If your program attempts to use a null pointer illegally in this way, the result is an error called a null pointer exception. When this happens while the program is running, an exception of type NullPointerException is thrown. After the computer executes these statements, the situation in the computer's memory looks like this:. In this picture, when a variable contains a reference to an object, the value of that variable is shown as an arrow pointing to the object.
Note, by the way, that the Strings are objects! The variable std3 , with a value of null , doesn't point anywhere. The arrows from std1 and std2 both point to the same object. This illustrates a Very Important Point:.
When one object variable is assigned to another, only a reference is copied. The object referred to is not copied. Instead, std2 was set to refer to the very same object that std1 refers to. This is to be expected, since the assignment statement just copies the value that is stored in std1 into std2 , and that value is a pointer, not an object.
But this has some consequences that might be surprising. For example, std1. After the string "Mary Jones" is assigned to the variable std1. There is a potential for a lot of confusion here, but you can help protect yourself from it if you keep telling yourself, "The object is not in the variable.
The variable just holds a pointer to the object. But the values that you are comparing are references to objects; they are not objects. So, you are testing whether std1 and std2 refer to the same object, that is, whether they point to the same location in memory. This is fine, if it's what you want to do.
But sometimes, what you want to check is whether the instance variables in the objects have the same values. To do that, you would need to ask whether " std1. I've remarked previously that Strings are objects, and I've shown the strings "Mary Jones" and "John Smith" as objects in the above illustration. Strings are special objects, treated by Java in a special way, and I haven't attempted to show the actual internal structure of the String objects.
Since strings are objects, a variable of type String can only hold a reference to a string, not the string itself.
Suppose that greeting is a variable of type String , and that it refers to the string "Hello". Well, maybe, maybe not. The variable greeting and the String literal "Hello" each refer to a string that contains the characters H-e-l-l-o. The function greeting. The fact that variables hold references to objects, not objects themselves, has a couple of other consequences that you should be aware of. They follow logically, if you just keep in mind the basic fact that the object is not stored in the variable.
The object is somewhere else; the variable points to it. Suppose that a variable that refers to an object is declared to be final. This means that the value stored in the variable can never be changed, once the variable has been initialized. The value stored in the variable is a reference to the object.
So the variable will continue to refer to the same object as long as the variable exists. However, this does not prevent the data in the object from changing. The variable is final , not the object. It's perfectly legal to say.
Next, suppose that obj is a variable that refers to an object. Let's consider what happens when obj is passed as an actual parameter to a subroutine. The value of obj is assigned to a formal parameter in the subroutine, and the subroutine is executed. The subroutine has no power to change the value stored in the variable, obj.
It only has a copy of that value. However, the value is a reference to an object. Since the subroutine has a reference to the object, it can change the data stored in the object. After the subroutine ends, obj still points to the same object, but the data stored in the object might have changed.
Suppose x is a variable of type int and stu is a variable of type Student. When writing new classes, it's a good idea to pay attention to the issue of access control. Recall that making a member of a class public makes it accessible from anywhere, including from other classes.
On the other hand, a private member can only be used in the class where it is defined. In the opinion of many programmers, almost all member variables should be declared private. This gives you complete control over what can be done with the variable.
You might be confused here so just consider them as same. At university its tought, that an Object Variable is: "a variable whose type is a class, and which holds a reference to an object that is an instance of that class". Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 8 years, 4 months ago. Active 10 months ago. Viewed 58k times. Does anyone know the meaning of this term? Improve this question. Faaris Cervon Faaris Cervon 1 1 gold badge 2 2 silver badges 5 5 bronze badges. Buy a new book. Preferably one that doesn't make up terms.
If I had to take a guess, the object variable is the object reference aka a pointer the memory location where the object resides. An object variable is a container that holds a reference to a specific instance of a class. Otherwise known simply as a "variable" or "member" — Robert Harvey.
0コメント