Skip to content Skip to navigation

Connexions

You are here: Home » Content » Java Syntax Primer

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of 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.

      What are tags? tag icon

      Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

    • External bookmarks
  • E-mail the authors
  • Rate this module (How does the rating system work?)

    Rating system

    Ratings

    Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

    How to rate a module

    Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

    (0 ratings)

Recently Viewed

This feature requires Javascript to be enabled.

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