Computer Science Canada Void |
Author: | Darkspider [ Wed Aug 11, 2004 12:45 pm ] |
Post subject: | Void |
Hi! I'm learning java and am wondering when and why you use void? The book does not explain the use of void. Also, how is "this" used in Java? Thanks! |
Author: | wtd [ Wed Aug 11, 2004 4:19 pm ] | ||||||
Post subject: | |||||||
void Java is an explicitly typed language. This means that not only do all variables and methods have a type which cannot be changed, but you as the programmer are required to specify that type. This can be seen when you declare a variable like:
Methods must also have a type. This is the type of the variable they return. However, it is not required that a method actually return anything. In this case we still have to explicitly state what type we're returning. This quandry is resolved by using the "void" type. this When writing a class, this is a reference (all object variables in Java are references) to the current object. Other languages may call it "self" or "Current", with the former being quite popular. Still others (Python and O'Caml come to mind) allow it to be defined by the programmer. Having "this" around is quite handy. Consider the following simple class. It describes a Name object which stores a person's first and last names.
The constructor in this case takes two strings and assigns them to the first and last name variables internal to the class. Since there is no other declaration of "first" and "last" in the constructor, the compiler can determine that "first" and "last" here are variables belonging to the object. However, "initFirst" and "initLast" are a bit cumbersome. So we'll shorten them to "first" and "last".
Now, since there is a declaration of "first" and "last" in the constructor, the variables of the same name belonging to the object can't be "seen", unless prepended with "this". Doing so disambiguates the situation. |
Author: | wtd [ Wed Aug 11, 2004 5:13 pm ] | ||||||||||
Post subject: | |||||||||||
super The "super" variable is similar in many ways to the "this" variable. Both are "special" in that we don't see where they come from. They just are. In a nutshell, "super" is a reference to the current object, but it behaves like an object of the parent class. First, though, let's add a method to the Name class previously defined as:
Once we create a new Name object:
We can't really do anything with it. So as an addition, let's give it the ability to be printed by adding in the toString() method which is called automatically by many other methods in the Java standard library.
That was easy. Now we can have code like:
Still not terribly useful, but it demonstrates the concepts well enough. Let's get a bit fancy. A formal name is a name, so it makes sense to have FormalName inherit from Name. This new class will add a string containing a title to be associated with the name.
Here we see two uses of "super". The first is in the constructor. Since "first" and "last" were appropriately declared "private" in the Name class, we cannot directly access them in the FormalName class, leaving us with no way to initialize them as we did in Name's constructor. Instead, we use "super" to pass those arguments along to Name's constructor, which handles the nitty gritty details of initializing the proper variables. With the toString() method we've overridden the method of the same name in the Name class. Thus we cannot directly access it to get the name as a string. However, using "super" it becomes possible to get that string and prepend it with the title. |
Author: | Darkspider [ Wed Aug 11, 2004 9:20 pm ] |
Post subject: | |
Thanks for the help! |
Author: | wtd [ Wed Aug 11, 2004 9:41 pm ] |
Post subject: | |
You're welcome. ![]() [mod:03dc1ee050]I've just got to thank wtd a lot for all this help and solutions in the Java Help section. Nice work dude, keep up the good job. --rizzix[/mod:03dc1ee050] |