Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Apr 8, 1997

Basic Java Syntax

Here's some basic Java syntax, with explanations:


A Java object is a container that holds variables and functions (Java programmers call functions "methods"). Since (references to) Java objects can be stored in variables, Java objects can also hold other Java objects.

There are two types of Java objects: arrays and class instances.

A class is a declaration that describes the members (i.e. methods and variables) its instances will contain. For example:

 class Employee {

      //some instance variables
      private double salary;
      private String name;

      // default printer method
      public void println() {
         System.out.println("Name: " + name);
         System.out.println("Salary: " + salary);

      // constructor initializes variable members
      public Employee(String n, double s) {
         salary = s;
         name = n;

      // two selector methods and a mutator method
      public String getName() { return name; }
      public double getSalary() { return salary; }
      public void setSalary(double amt) { salary = amt; }

      // etc.

When we declare objects to be instances of the class Employee, we call new to allocate heap space for the objects, then invoke the Employee constructor to initialize the member variables:
 Employee smith = new Employee("Smith", 30000),
            jones = new Employee("Jones", 20000);
If we reassign Smith:
 smith = null;
and there are no other references to the original object bound to Smith, this object becomes garbage. When heap space runs low, Java's garbage collector will automatically be called to recycle all garbage.


While a method can freely refer to any other method or variable in the same instance, only methods and variables declared "public" can be accessed by methods in other instances. Since several instances of the same class can exist simultaneously, referencing a public method or variable from outside an instance requires qualifying the reference with a reference to the instance. For example:

invokes Smith's public setSalary() method, while
invokes jones' setSalary() method. But the scope of
includes only members of the Employee class.


Through inheritance, objects can have generalizations and specializations. For example, in addition to the methods and variables common to all employees, a secretary might also contain variables indicating typing speed and position:

 class Secretary extends Employee {
      private int wpm; // typing speed in words/minute
      private String position = "secretary";

      public void println() {
         super.println();  // calls Employee.println()
         System.out.println("Position: " + position);
         System.out.println("Typing speed: " + wpm);

      public String getPosition() { return position; }

      // etc.
Managers too are employees (although they sometimes seem to forget). Their distinguishing features might include a private secretary:
  class Manager extends Employee {
      private Secretary secretary; // objects can contain objects!
      private String position = "manager";

      public void println() {
         super.println();  // calls Employee.println()
         System.out.println("Position: " + position);
         System.out.println("Secretary: " + secretary.getName());

      public String getPosition() { return position; }
      public Secretary getSecretary() { return secretary; }
      public void setSecretary(Secretary sec) { secretary = sec; }

      // etc.


Suppose we represent companies by objects:

 class Company { ... }
Inside we might declare an array of N employees:
 Employee[] staff = new Employee[N];
Filling this array with various types of employees requires no explicit casts:
staff[0] = new Secretary(...);
   staff[1] = new Programmer(...);
   staff[2] = new Manager(...);
   // etc.
But specializing from an employee does require an explicit cast, which should only be done after the programmer verifies the specialization is valid:
 if (staff[0] instanceof Secretary && staff[2] instanceof Manager)


Like C++ virtual functions, Java methods are dynamically bound. Each call to println() in the following "for" loop automatically invokes the appropriate specialized println() method:

for(int i = 0; i < N; i++)
      staff[i].println();  // prints name, salary, position, etc.
This statement compiles because the Employee base class contains a default println() method. The absence of a default getPosition() method in the Employee base class prevents compilation of the statement:
for(int i = 0; i < N; i++) 
      if (staff[i].getPosition().equals("manager"))
         managerCount += 1;
To correct this problem we can add an abstract getPosition() method to the Employee base class:
abstract class Employee {
      // ...
      public abstract String getPosition();  // no body
The parallel construct in C++ is a pure virtual function:
class Employee {
         virtual String getPosition() = 0;  // no body!
      // etc.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date