Google+

Android4Beginners.com

Apps development for Android step by step

Lesson 0.10: Java basics – first contact with Java programming language (objects, classes, variables and methods)

As we have already learnt from previous lessons Android apps are written in Java – one of many programming languages (for instance apps for Apple iOS are written in Objective C). We will try to understand some basic concepts of Java.

Objects and classes

Java is based on objects. We know objects from real life – a car could be an object, a book could be an object or human could be an object. Objects have some features or parameters. For car it could be speed and color, for book – title and number of pages – and for human sex and marriage status. We called this features states in Java.

A car is an object in a real life, so it could be in Java. We could create a class Car that would be a template for car objects in Java  (Photo credit SXC.hu/)

A car is an object in a real life, so it could be in Java. We could create a class Car that would be a template for car objects in Java (Photo credit SXC.hu/)

We could say that definition of objects in Java is done by classes. You could treat a class as a template (blueprint) for an object. You could create many objects based on one class (objects based on a common template). Those copies are called instances. Kate, Alex and John could be instances of human object.

A class has its name and also access information set to public (we will go more deeply into this later).  All class code is kept in brackets { }.

For instance:

Variables (fields)

To store values of object features (states) we use variables (called also fields). There are various variables types, but at the beginning let’s start with just three: int (for keeping integer numbers , like 5), String (for text, like “Amanda” ) and boolean that keeps logic values (true or false). We could set some specific values for variables from the beginning or just define them and set values later.

We use = sign to set a value to variable, for instance color = “red” (you could read it as color becomes red). You should not confuse it with math sign = which means “equals”.

Two more important remarks. In human language we use dot (.) to finish sentences, in Java we use semicolon (;) instead. And it matters if you use capital or small letters in Java keywords (like class, int, boolean, but String).

There are good practices for writing names of classes and variables (though not obeying them won’t result in an error). Start class names with capital letters and variable names with small ones. In both cases second element of a name should be started with capital letter (like SportCar for class and maxSpeed for variable).

Our classes with variables would look like this:

As you see for some variables we have already assigned specific values, for others not. Probably it’s obvious, but of course we could have as many variables in a class as we wish. If variables are the same type, we could list them after colon.

For instance:

Methods (behaviors)

To interact with classes we use methods (they are also called behaviors). If we want method to return some result, we have to declare method type (there are the same types as for variables), otherwise we use keyword void. Methods have their own code inside {} as classes. Moreover methods could receive some arguments, but we explore it later. It’s advised to write method names in the same way as variables (start with small letter), but we add brackets after name.

For instance:

The first method modifies a car speed. This example illustrates also why we shouldn’t read = sign as “equals” (from math we know that speed can’t equal speed + 5), but we should read is rather as “becomes”. The second method returns a car color.

So our Car class with variables definition and methods would look like this:

Mind which brackets start and finish class code and which ones are for methods.

Adding a class to Android Studio project

If you would like to add a new class to your app project, follow those steps:

Step 1. In Java folder right click on com.example.myfirstandroidapp (name depends on your app name).

Step 2. Choose New and then Java Class.

Usually we add new Java classes separately to the app package, though it's also possible to have inner classes in other class (Android Studio)

Usually we add new Java classes separately to the app package, though it’s also possible to have inner classes in other class (Android Studio)

Step 3. You have to specify class name (for exmaple Book). Leave Kind option do default value (Class). Click OK.

Step 4. Now you should see a window with a class code. Moreover there should be Book class visible in project tree.

After adding a new class, you should see its code on the screen as well a new position in the project tree (Android Studio)

After adding a new class, you should see its code on the screen as well a new position in the project tree (Android Studio)

Summary: This is just very entry level introduction to objects, classes, variables and methods. We’d go more deeply into them in next lessons. For now it’s good to remember that a class could be used as a template for creating many instances of specific object. Class saves its states in variables of different types. And you do some operations via methods. Moreover every Java sentence (line of code, group of instructions) has to be finished with semicolon.

Previous:
Next: