devxlogo

Basic Java Syntax

Basic Java Syntax

Question:
Here’s some basic Java syntax, with explanations:

Answer:
OBJECTS, CLASSES and INSTANCES

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.

METHOD INVOCATION

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:

 smith.setSalary(100000);
invokes Smith’s public setSalary() method, while
   jones.setSalary(10000);
invokes jones’ setSalary() method. But the scope of
smith.salary
includes only members of the Employee class.

INHERITANCE

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

GENERALIZATION AND SPECIALIZATION

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)      (manager)staff[2].setSecretary((Secretary)staff[0]);

DYNAMIC BINDING

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 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 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 {      public:         virtual String getPosition() = 0;  // no body!      // etc.   }
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist