What You'll Learn

Java is case sensitive.

Java convention for the naming and writing of the class, method, variable.


Rule for naming



Upper case. It should be name



lower case. It should be starting or being a verb



lower case. It should be short but meaningful

message = "this is a message"

link for some naming conventions

The focus need to change when thinking about OOP paradigm as everything can and should become an object.

A shift of way of thinking is needed to be able to understand that everything in this programming paradigm could be object. The parallel could be done with real life, and as any concepts, real object, living creature. For example, a program that manage the student's registration will have objects such as "student", "teacher", "class", "module". Objects could also represent more abstract entities, like an object that represents an "open file", or an object that provides the "service of translating a language" from English to Welsh. These objects will have some common properties and behaviours. For example, "student" will have common properties such as forename, a surname, a date of birth. The will have the same behaviour (for the system), such as registering to one or several modules, registering to an university, getting marks. These properties and behaviours are conceptualised from the requirements of the system, not from the general knowledge of the object in itself.

Object images


The declaration of the class start with the level of accessibility of the class with a modifier: public, protected, no modifier, private.

A class is often in public as it will certainly be reach from another class, external to the declared class.

It will be followed by the keyword class (in lower case), will be followed by the name of the class. It will then be followed by an open curly brackets, and the class need to be closed by a close curly brackets.

tips: always check your open and close brackets, it will often be one of the errors from the Java console.

This name will start with an upper case and should be meaningful to what the class represent. For example, a class that represent a student should be named "Student".

The name of the class is the same than the name of the file, and Java is case sensitive. tips: always check the name of your class and your file, be sure that the case are the same and the name is the same.

public class Message {

For this example, the name of the file need to be Message.java

First create a folder name First_exercise_java

In this folder, create a file Message.java

Re-write the following code and save the file.

The declaration of the method will start with the level of accessibility specified by a modifier (public, protected, no modifier, private). It will be followed by the type that this method will return or by void, if this method doesn't return anything. It will be followed by the name of the method. The usage of a verb in the name of the method is good practice. Followed by the parentheses, which are used to setup the arguments of the method, if the methods has no arguments, the parentheses are empty. Similar to the class declaration, it will be followed by an open curly bracket. The closed curly bracket will be used to close the method. In java, a end of line of command is finishing by a ";".

In the method, the behaviour of the methods is defined. In this example, a variable of type String is declared (Java is a strong typed programming language), followed by the name of the variable. The assignment operator for is "=", which will assign the String "hello World" to the variable message.

In a next step, if the signature of the method contains a return value and not void, you need to return a value. In this example, the signature method contains void, by consequence the method is not returning anything. If the method was returning a value, the keyword return should be used followed by the variable.

tips: Always check that the type of variable you are returning is the same type that the one you defined in the signature of the method or that it doesn't return anything if the method return a void.

public class Message {

	public void getMessage() {
	String message = "hello World";

You create your first class in Java. This class has one method, and this method print "hello World".

In the folder First_exercise_java create a file FirstExercise.java

You will have to declare the class FirstExercise. This class is also public.

You need to implement the main method. The main method is a special method in Java. Every Java application must have a main method, as this is the entry point of your application.

Re-write the following code and save the file.

public class FirstExercise {

	public static void main (String args[]) {
	//creation of an instance of the class message with no parameter in the signature of the constructor
      Message firstMessage = new Message();
	//call the method printMessage on the instance of the object Message
	//and store the returned String in a String variable



In this second class FirstExercise.java, you are creating an instance of the class message, using the default constructor of the class Message.java in the main method.

See the image below for the vocabulary of the actions:

Object images

Go to your Terminal (Gitbash)

Go in the directory that contains the two previous file: First_exercise_java

Make sure that both files are in it and type the following:

javac FirstExercise.java

If you don't have errors that appear in your Shell you just have compiled your first java application

Now you want to run it, type the following:

java FirstExercise

You should see this in your Shell

First Message screenshot

You can find the code for this example in https://git.cardiff.ac.uk/ASE_GROUP_2020/code_for_codelabs.git