<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="Module.2003-12-16.0037">
	<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Java Syntax Primer</name>
	<metadata xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
  <md:version xmlns:bib="http://bibtexml.sf.net/">**new**</md:version>
  <md:created xmlns:bib="http://bibtexml.sf.net/">2003/12/16 01:00:37.188 US/Central</md:created>
  <md:revised xmlns:bib="http://bibtexml.sf.net/">2003/12/16 01:03:02.721 US/Central</md:revised>
  <md:authorlist xmlns:bib="http://bibtexml.sf.net/">
    <md:author xmlns:bib="http://bibtexml.sf.net/" id="swong">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Stephen</md:firstname>
      
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Wong</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">swong@rice.edu</md:email>
    </md:author>
    <md:author xmlns:bib="http://bibtexml.sf.net/" id="dxnguyen">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Dung</md:firstname>
      <md:othername xmlns:bib="http://bibtexml.sf.net/">X.</md:othername>
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Nguyen</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">dxnguyen@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist xmlns:bib="http://bibtexml.sf.net/">
    <md:maintainer xmlns:bib="http://bibtexml.sf.net/" id="swong">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Stephen</md:firstname>
      
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Wong</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">swong@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer xmlns:bib="http://bibtexml.sf.net/" id="dxnguyen">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Dung</md:firstname>
      <md:othername xmlns:bib="http://bibtexml.sf.net/">X.</md:othername>
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Nguyen</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">dxnguyen@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist xmlns:bib="http://bibtexml.sf.net/">
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">Java, syntax, object oriented programming</md:keyword>
  </md:keywordlist>

  <md:abstract xmlns:bib="http://bibtexml.sf.net/">An introduction to Java syntax covering simple Java expressions and the declaration of concrete classes and methods.     </md:abstract>
</metadata>
	<content xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
		<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="p10"><!-- Insert module text here -->This module contains an assortment of topics covering Java syntax.   It is not intended to be a complete tutorial.</para>
		<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10">
			<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Constructors</name>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10p10">A constructor can be thought of as a specialized method of a class that creates (instantiates) an instance of the class and performs any  initialization tasks needed for that instantiation.   The sytnax for a constructor is similar but not identical to a normal method and adheres to the following rules:<list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10p10l10">
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The name of the constructor must be exactly the same as the class it is in.  </item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Constructors may be private, protected or public.</item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">A constructor <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">cannot </emphasis>have a return type (even <foreign xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">void</foreign>).  This is what distinguishes a constructor from a method.  A typical beginner's mistake is to give the constructor a return type and then spend hours trying to figure out why Java isn't calling the constructor when an object is instantiated.</item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Multiple constructors may exist, but they must have different signatures, i.e. different numbers and/or types of input parameters.</item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The existence of any programmer-defined constructor will eliminate the automatically generated, no parameter,  <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">default constructor</emphasis>.   If part of your code requires a no parameter constructor and you wish to add a parameterized constructor, be sure to add another, no-parameter constructor.</item>
				</list></para>
			<example xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10e10">
				<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10e10p10"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">public Person {  </code><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">    private String _name; </code><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">    public Person(String name) { </code><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">        _name = name; </code><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">    } </code><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">}</code></para>
				<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10e10p20">The above code defines a public class called <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Person </code>with a public constructor that initializes the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">_name</code> field. </para>
			</example>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10p20">To use a constructor to instantiate an instance of a class, we use the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">new </code>keyword combined with an invocation of the constructor, which is simply its name and input parameters.  <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">new </code>tells the Java runtime engine to create a new object based on the specified class and constructor.  If the constructor takes input parameters, they are specified just as in any method.   If the resultant object instance is to be referenced by a variable, then the type of the newly created object must be of the same type as the variable.   (Note that the converse is not necessarily true!).   Also, note that an object instance does not have to be assigned to a variable to be used.   </para>
			<example xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10e20">
				<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10e20p10"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">Person me = new Person("Stephen");</code></para>
				<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10e20p20">The above code instantiates a new <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Person </code>object where <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">_name</code>field has the value "<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Stephen</code>".</para>
			</example>
		</section>
	</content>
</document>
