Syntax

Welcome to the syntax page. Post Java syntax you know of here:

Class

class ClassNameIdentifier {  } 

Using access modifiers:

public class ClassNameIdentifier {  } 
private class ClassNameIdentifier {  } 
protected class ClassNameIdentifier {  } 

With inheritance:

class ClassNameIdentifier extends AnotherClass {  } 

NOTE: A class may extend one and only one other class.[1]

Using an interface:

class ClassNameIdentifier implements SomethingInterfaceable {  } 

Or multiple interfaces:

class ClassNameIdentifier implements ThisInterface, ThatInterface{  } 

And a class that should not be instantiated is abstract:

abstract class ClassNameIdentifier {  } 

A class that canot be extended:

final class ClassName { } 

Method

void methodName() { } 

Using a primitive data type,

int methodName() {  return 0; } 

Returning an object,

String methodName() {  return "method return"; } 

A public method,

public String methodName() {  return "method return"; } 

A private method,

private String methodName() {  return "method return"; } 

A package-private method,

protected String methodName() {  return "method return"; } 

A method with parameters,

String greet(String userName) {  return "Hello " + userName; } 

A static method,

static String sayHello() {  return "Hello"; } 

Overriding a method,

@Override public String toString() {  return "method return"; } 

Calling a Method

methodName(); 

Calling a static method of a different class,

Math.random(); 

Calling an object's method (assume it was constructed),

anObject.methodName(); 

Printing to standard out (console),

System.out.println("insert text here"); //with line return System.out.print("insert text here too"); //without line return 

Variable

Declaring a variable (of type int),

int aWholeNumber; 

Initializing a variable,

String userName; userName = "bob"; double busFare = 4.25; 

Declaring an object,

Object anObject; 

Constructing an object,

Object anObject = new Object(); 

Flow Control

When parts of the code are executed on certain conditions or states. Each of these constructs may be nested within one another (see Syntax#Nesting)

Conditional

if(someCondition) {  //execute when true } 
if(someCondition) {  //execute when true } else {  //execute when false } 

Multiple conditions,

if(someCondition) {  //execute only this block  //when someCondition is true } else if(someOtherCondition) {  //execute only this block  //when someOtherCodition is true } else {  //execute when all others are false } 

Better scaling multiple conditions,

char singleCharChoice; switch(singleCharChoice) {  case 'A':  //statements if this is case  break;  case 'B': case 'b':  //statements when either case is true  break; } 

Including a default case,

char singleCharChoice; switch(singleCharChoice) {  case 'A':  //statements if this is case  break;  case 'B': case 'b':  //statements when either case is true  break;  default:  //statements when nothing else holds true } 

Loop

while(someCondition) {  //statements to repeat  //make sure you can get out of the loop! } 

When the statements need to execute at least one time,

do {  //statements to repeat  //make sure you can get out of the loop! } while(someCondition); 

When the exact number of iterations is known,

for(int i = 0; i < aKnownLimit; i++) { } 

Nesting

do {  //statements to repeat  //make sure you can get out of the loop!  if(aSeparateCondition) {  //a conditional within a loop!  while(anotherCondition) {  //looping within a conditional within a loop!  }  } } while(someCondition); 

Package and Imports

To specify a class is a part of a package, the first line in a class file should have a line similar to the following,

package custom.package.path.name; 

To import members from other packages, include something similar to the following,

import other.package.path.name.ClassMember; 

To import all members within a package, use the character,

import java.util.*; 

To import a static method

import static other.package.name.ClassName.methodName 

To import an inner class

import other.package.name.ClassName.InnerClassName 

External sites

References

  1. Oracle. "What Is Inheritance?". Accessed 2014-10-22 14:22:29UTC

Wikipedia

  1. Wikipedia contributors, "Java syntax," Wikipedia, The Free Encyclopedia, http://en.wikipedia.org/w/index.php?title=Java_syntax&oldid=618807932 (accessed October 23, 2014).
Community content is available under CC-BY-SA unless otherwise noted.