Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Void
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Darkspider




PostPosted: 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!
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Aug 11, 2004 4:19 pm   Post subject: (No 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:

code:
String myStringVariable;


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.

code:
class Name {
   private String first;
   private String last;

   public Name(String initFirst, String initLast) {
      first = initFirst;
      last  = initLast;
   }
}


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

code:
class Name {
   private String first;
   private String last;

   public Name(String first, String last) {
      this.first = first;
      this.last  = 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.
wtd




PostPosted: Wed Aug 11, 2004 5:13 pm   Post subject: (No 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:

code:
class Name {
   private String first;
   private String last;

   public Name(String initFirst, String initLast) {
      first = initFirst;
      last  = initLast;
   }
}


Once we create a new Name object:

code:
Name bobsName = new Name("Bob", "Smith");


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.

code:
class Name {
   private String first;
   private String last;

   public Name(String initFirst, String initLast) {
      first = initFirst;
      last  = initLast;
   }

   public String toString() {
      return first + " " + last;
   }
}


That was easy. Now we can have code like:

code:
Name bobsName = new Name("Bob", "Smith");
System.out.println(bobsName);


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.

code:
public class FormalName extends Name {
   private String title;

   public FormalName(String title, String first, String last) {
      super(first, last);
      this.title = title;
   }

   public String toString() {
      return title + " " + super.toString();
   }
}


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




PostPosted: Wed Aug 11, 2004 9:20 pm   Post subject: (No subject)

Thanks for the help!
wtd




PostPosted: Wed Aug 11, 2004 9:41 pm   Post subject: (No subject)

You're welcome. Smile


[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]
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: