Connexions

You are here: Home » Content » Java Syntax Primer
Content Actions
Lenses

What is a lens?

Lenses

A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual Connexions member, a community, or a respected organization.

This content is ...
Affiliated with (?)
This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • This module is included inLens: Rice University OpenCourseWare
    By: OpenCourseWare ConsortiumAs a part of collection:"Principles of Object-Oriented Programming"

    Click the "Rice University OCW" link to see all content affiliated with them.

    Rice University OCW
Tags

(?)

These tags come from the endorsement, affiliation, and other lenses that include this content.

Java Syntax Primer

Module by: Stephen Wong, Dung Nguyen

Summary: An introduction to Java syntax covering simple Java expressions and the declaration of concrete classes and methods.

This module contains an assortment of topics covering Java syntax. It is not intended to be a complete tutorial.

Constructors

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:
  • The name of the constructor must be exactly the same as the class it is in.
  • Constructors may be private, protected or public.
  • A constructor cannot have a return type (even void). 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.
  • Multiple constructors may exist, but they must have different signatures, i.e. different numbers and/or types of input parameters.
  • The existence of any programmer-defined constructor will eliminate the automatically generated, no parameter, default constructor. 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.
Example 1 
public Person {  
    private String _name; 
    public Person(String name) { 
        _name = name; 
    } 
}
The above code defines a public class called Person with a public constructor that initializes the _name field.
To use a constructor to instantiate an instance of a class, we use the new keyword combined with an invocation of the constructor, which is simply its name and input parameters. new 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.
Example 2 
Person me = new Person("Stephen");
The above code instantiates a new Person object where _namefield has the value "Stephen".

Comments, questions, feedback, criticisms?

Send feedback