Time Zone

I love long walks on clichés.. “Time Zone” is published by Sarah O'Brien.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Exception Handling

If you want to start ‘Android Development’ but don’t know enough java, then this article is for you.

For Android Development you don’t need to know much about the different algorithms and various data-structures, but you need to have a knowledge of the following concepts(explained in detail later in this article):

Before any Java you need to know some key differences between Java and C/C++:

Some concepts to be kept in mind while writing any Java Program:

Most useful aspect of OOPs used in android is of Polymorphism. It simply means the ability of one object or method(function) to serve multiple purposes.

Interfaces

Carefully observe the declaration of main method. Main method can’t be abstract (as the execution of main method starts from the definition of the main method) . Static keyword(used in the prototype of main method) is used for classes, methods and variables. It specifies that the method/variable is not dependent on any object and without creating the object of the class we can call that method by specifying the class name. Within a static method or static block you can only access static data members.

As there is no need for ‘schoolName’ to be dependent on object, hence it should be assigned static.

Now what are interfaces? Interface is just a contract between the one who needs certain features and between the one who is ready to give provide those features. Assume that there is a person and wants an app to be build, then he would create an interface(purely abstract class which plays the role of a contract) specifying all the methods(functions) that the app should have. By default all the methods in the interface are public and abstract(whether you specify it or not) and every interface variable is always public static final whether we are declaring or not.

As the name suggest a class within another class in called inner class. Whenever there is a condition that an object can’t exist without the existence of another type of object we should go for inner classes. Inner classes cannot have static declaration.

Most widely used inner classes in android are Anonymous inner classes. Anonymous inner classes are nameless classes which are created just for instant purpose(one-time purpose).

Suppose there is a class with class name as ‘ClassName’. When we do :

This statement creates an object of ClassName. But when we do:

We create an object of a class which is nameless and extends ClassName. If instead ‘ClassName’ is an interface then we end up creating an object of the class which does not have any name and implements ClassName interface. In that case the inner class must implements all the methods of the interface ClassName.

Let’s take an example from android :

In mobile apps generally we generally come across buttons which have some task to do when they are clicked. We define these tasks by defining the following method(which is present in the View class of android) :

As the above method is NOT static we have to call the above method only by creating a View class object and then calling the above method solves our purpose. But before defining the above method we need to understand the argument “View.OnClickListener” .

OnClickListener is an interface and we need to send an object of the class which implements this interface to our setOnClickListener method.(Remember: Interfaces cannot be instantiated).

Implementing the above interface means defining the onClick(View v) method. As this is a one-time requirement we usually go for Anonymous Inner Classes for this implementation. Button is a class in android which is a subclass of View class.

which is equivalent to :

In the above code:

this line creates a class which does not have any name and implements the interface View.OnClickListener().

Exception handling simply means to handle an exception so that continuous flow of program can be maintained. Exceptions are of three types :

3. Error(abnormal termination of the program which can’t be handled by the programmer). If a program needs some memory to store its data but the user’s hard disk is full, then in this scenario an error will occur.

It is generally accomplished by :

try-catch-finally block(recommended) :

throws keyword(not recommended) :

‘throws’ keyword doesn’t prevent the abnormal termination of the program.

Multithreading is used to achieve multitasking feature of java. It can be achieved by two ways :

Advantage of Multithreading:

The main advantage of multithreading is that it prevents the blockage of UI (prevents your app from becoming unresponsive) because threads are independent and you can perform multiple operations at same time and also it helps to perform many operations together and hence saves time.

final: final keyword is used to make the reference consistent. final seems to be same as the ‘const’ keyword of C++ but they have a huge difference. ‘const’ keyword is used to make the ‘value’ constant but ‘final’ is used to make the ‘reference’ constant. ‘final’ is a keyword which is applicable for classes, methods and variables. If a class is final then it cannot be inherited. If a method is final then it cannot be overridden. If a variable is final then its value can be changed but not its reference. If the variable is immutable then its value can also be not changed (in that case it is similar to const keyword of C++) , but if the variable is mutable then its value can be changed.

The above code will give Compilation Error saying ‘marry() in Derived cannot override marry() in Base’.

The format with a square bracket is because internally System.out.println is calling toString() method which is present in the ArrayList class. See how we changed the value of the Collection ‘ArrayList’ though it is ‘final’.

As primitive data types are immuted , final keyword behaves similar to const keyword in C++ . We have seen how ‘abstact’ keyword works and how ‘final’ keyword works and hence they forms an illegal combination if they are put together. If a method is declared as abstract then its implementation must be provided in its child classes and if a method is declared as final then its implementation must be given there only and that method can’t be overriden. If a class is declared as abstract then it must be inherited and if a class is final then it must not be inherited and hence ‘abstract and final’ forms an illegal combination and can’t be operated simultaneously. An ‘abstract’ class can contain final methods but a ‘final’ class can’t contain abstract method.

synchronized: It is modifier applicable for methods ad blocks but not for classes and variables. synchronized’ is used to make some data ‘thread safe’, which means, that only one ‘thread’ can operate on the data at a time. Thread simply means the path followed when executing a program. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block. The main disadvantage of synchronized keyword is that it increases waiting time of threads.

Some key points to be considered while writing code is java

In java ‘top level classes’ can only have the following keywords: public, abstract, strictfp , default and final (you can remember these keywords by letters: a, s, d, f written continuously on ‘qwerty keyboard’ in the middle row). Default is when no keyword is mentioned before ‘class’.

If a class is not ‘top level classes’ then it can also be ‘protected private static ’. (The strictfp keyword is used to force the precision of floating point calculations in Java conform to IEEE’s 754 standard, explicitly. Without using strictfp keyword, the floating point precision depends on target platform’s hardware.)

According to me, this much java is necessary and sufficient for any android development beginner. Thanks for reading this article.

Add a comment

Related posts:

Love is in the Air

The damned light was burning through my eyelids, just like the last time. I was probably still alive. No reason to give it away just yet. I tried to stay still, avoid “treatment” as long as possible…

Unspoken Bond

You believed in me when I had lost faith in myself. You saw a potential future for me when I had given up hope. You stood behind me like a rock when I was ready to give up. You kept me close, even…

Tulisan Pertama di Medium

Barangkali dunia tulis menulis saat ini telah bertransformasi menjadi sebuah alat yang cukup lumrah di kalangan masyarakat. Sayangnya, perkembangan media sosial jauh lebih cepat dan masif ketimbang…