<< Chapter < Page Chapter >> Page >
An introduction to Java syntax covering simple Java expressions and the declaration of concrete classes and methods.


A Java program consists of one or more classes one of them must be public and must have a method with the following signature:

public static void main (String[] args)

Basically, the main() method will instantiate appropriate objects and send them "messages" (by calling their methods) to perform the desired tasks.  The main() method should not contain any complicated program logic nor program flow control. In the example shown below, PizzaClient is the main class with the main() method.

UML Class Diagram of a Java program


public class PizzaClient { /*** Prints the answer to the problem stated in the above.. */public void run() { Pizza round = new Pizza (3.99, new Circle (2.5));Pizza rect = new Pizza (4.99, new Rectangle (6, 4)); PizzaDeal pd = new PizzaDeal();System.out.println(round + " is a better deal than " + rect + ": " + pd.betterDeal(round, rect));} /*** Main entry to the program to find the better deal. * Instantiates an instance of PizzaClient and tells it to run.* This is what all main() should do: instantiates a bunch of objects and * "turn them loose"!* There should be no complicated logic and/or control in main(). * @param nu not used*/ public static void main (String[]nu) { new PizzaClient().run();} }public class Rectangle implements IShape { private double _width;private double _height; /*** Initializes this Rectangle width a given width and a given height * @param w width of this Rectangle,>= 0. * @param h height of this Rectangle,>= 0. */public Rectangle(double w, double h) { _height = h;_width = w; }/** * @returns this Rectangle's area.*/ public double getArea() {return _height * _width; }/** * Overrides the inherited method from class Object.* @returns a String describing a Rectangle with its width and height. */public String toString() { return "Rectangle(h = " + _height + ", w = " + _width + ")";} }

Notes on the toString() method:

toString() is a method that is inherited all the way from the base class, Object. It is the method that the Java system calls by default whenever a string representation of the class is needed. For instance, "This is "+ myObject is equivalent to "This is " + myObject.toString() . DrJava will call an object's toString() method if you type the object's name in the interaction window, without terminating the line with a semicolon. The return value of toString() is what prints out on the next line.

Comments syntax:

// Line-oriented - comment goes to end of the current line.

/* block-orientedcan span several lines. */

Class definition syntax:

[…] means optional .

[public] class class-name [inheritance-specification]{ [field-list;][constructor-list;] [method-list;]


Inheritance Specification looks like

[extends SomeClass] [implements Interface1,..., InterfaceN]

We will discuss the meaning of inheritance in due time.

Java statement syntax:

Each Java statement must terminate with a semi-colon. 

Field list syntax:

A field list consists of zero or more field declarations of the form

[static] [final][public | private | protected] field-type field-name [assignment];


Constructor list syntax:

A constructor list consists of zero or more constructor definitions of the form

[public | private | protected] class-name ([parameter-list]){ [statement-list;]}
The constructor's name must the same as the class name. Constructors are used for initialization of the object during the object's instantiation only.

Method list syntax:

A method list consists of zero or more method definitions of the form

[static] [final][public | private | protected] return-type method-name ([param-list]) { [statement-list;]}

A return type void means the method does not return any value.

param-list can be:

  • empty;

for example,

public double getArea() { // code ...}
  • of a fixed number of parameters

type1 param1, type2 param2, ... , typeN paramN

for example,

private void doSomethingWith(int n, double x, Pizza p) { // code ...}
  • of a variable number of parameters ( this is a new feature for Java 5.0 and upward )
type1 param1, type2 param2, .., typeN... params

In the above, typeN... params is called a variable argument list.  It must appear at the end of the whole parameter list.

for example,

private void doSomethingWith(int n, double x, Pizza... p) { // code ...}

The variable argument list can have zero or more arguments.  Here is a simple example:  use DrJava to define the following class in the definition pane.

public class VarArgExample { /*** returns the number of arguments in the variable argument list. */int argNum(int... nums) { return nums.length; // the var arg list is viewed as an array} }

Compile the above class and in the interactions pane, type the following:

VarArgExample vae = new VarArgExample(); // creates a VarArgExample object vae.argNum() // returns 0vae.argNum(42) // returns 1 vae.argNum(34, -99) // returns 2

Questions & Answers

so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
what is system testing
what is the application of nanotechnology?
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
anybody can imagine what will be happen after 100 years from now in nano tech world
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
silver nanoparticles could handle the job?
not now but maybe in future only AgNP maybe any other nanomaterials
I'm interested in Nanotube
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
can nanotechnology change the direction of the face of the world
Prasenjit Reply
At high concentrations (>0.01 M), the relation between absorptivity coefficient and absorbance is no longer linear. This is due to the electrostatic interactions between the quantum dots in close proximity. If the concentration of the solution is high, another effect that is seen is the scattering of light from the large number of quantum dots. This assumption only works at low concentrations of the analyte. Presence of stray light.
Ali Reply
the Beer law works very well for dilute solutions but fails for very high concentrations. why?
bamidele Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?