This is the third session in android course that started with java syntax. This presentation have many topics like ( Exception and Try .. Catch ….. finally, Classes & Objects, Naming conventions, Access Modifiers, Non-Access Modifiers, Encapsulation )
3. Exception and Try .. Catch ... finally :-
• Exception, events that occur during the execution of programs that
disrupt the normal flow of instruction.
• In Java, it is an object that wraps an error event that occurred within a
method and contains:
➢ Information about the error including its type.
➢ The state of the program when the error occurred.
➢ Optionally, other custom information.
• EX:
throw new NullPointerException("demo");
4. Exception and Try .. Catch ... finally :-
• Exceptions have many different types of error conditions.
➢ JVM Errors:
▪ OutOfMemoryError
▪ StackOverflowError
▪ LinkageError
➢ System errors:
▪ FileNotFoundException
▪ IOException
▪ SocketTimeoutException
➢ Programming errors:
▪ NullPointerException
▪ ArrayIndexOutOfBoundsException
▪ ArithmeticException
6. Exception and Try .. Catch ... finally :-
• Try, allow programmer to check error in a block of code.
• Catch, allows programmer to define a block of code to be executed, if an
error occurs.
• Finally, statement lets you execute code, after try...catch, regardless of
the result.
• Syntax :
try { // Block of code to try
} catch(Exception e) { // Block of code to handle errors
} finally { System.out.println("The 'try catch' is finished.");
}
7. try and catch ….finally :-
• EX :
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
8. Classes & Objects :-
• Class, consist of constructor, attribute and methods.
• public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
9. Classes & Objects :-
• Class Attributes :
public class Student {
int age = 5;
String name = 3;
}
• Object, is created from a class.
• EX :
MyClass myObj = new MyClass();
10. Classes & Objects :-
• Method, is a block of code which only runs when it is called.
• Syntax :
void myMethod() {
System.out.println("I just got executed!");
}
EX :
Calling in main method :
public static void main(String[] args) {
myMethod();
}
12. Classes & Objects :-
• Constructor, special method that called when an object of a class is
created. It can be used to set initial values for object attributes
• EX:
public class MyClass {
int x;
public MyClass() {
x = 5;
}
}
13. Classes & Objects :-
• EX:
public class MyClass {
int x;
public MyClass( int y) {
if (x==null)
x=0;
x=x+y;
}
}
15. Naming conventions :-
• Naming convention, is a rule suggested to follow as you decide what to
name your identifiers such as class, package, variable, constant,
method,.... .
• Naming for class :
➢ The name must not contain any white spaces.
➢ The name should not start with special characters like ( &, $, _ ) .
➢ It should start with the uppercase letter.
➢ It should be a noun such as Color, Button, System, Thread, etc.
➢ Use appropriate words, instead of acronyms.
16. Naming conventions :-
• Naming for Interface :
➢ It should start with the uppercase letter.
➢ It should be an adjective such as Runnable, Remote, ActionListener.
➢ Use appropriate words, instead of acronyms.
• Naming for Method :
➢ It should start with lowercase letter.
➢ It should be a verb such as main(), print(), println().
➢ If the name contains multiple words, start it with a lowercase letter followed by an
uppercase letter such as actionPerformed().
17. Naming conventions :-
• Naming for Variable :
➢ It should start with a lowercase letter such as id, name.
➢ It should not start with the special characters like & (ampersand), $ (dollar), _
(underscore).
➢ If the name contains multiple words, start it with the lowercase letter followed by an
uppercase letter such as firstName, lastName.
➢ Avoid using one-character variables such as x, y, z.
• Naming for Package :
➢ It should be a lowercase letter such as java, lang.
➢ If the name contains multiple words, it should be separated by dots (.) such as java.util,
java.lang.
18. Naming conventions :-
• Naming for Constant :
➢ It should be in uppercase letters such as RED, YELLOW.
➢ If the name contains multiple words, it should be separated by an underscore(_) such
as MAX_PRIORITY.
➢ It may contain digits but not as the first letter.
• Camelcase & Pascalcase:
➢ Classes: PascalCase — class VectorImage {}
➢ Methods: camelCase — drawImage()
➢ Variables: camelCase — string newImageName
19. Access Modifiers :-
• Access Modifiers, controls the access level.
➢ Public, The class is accessible by any other class.
➢ Private, The code is only accessible within the declared class.
➢ Protected, The code is accessible in the same package and subclasses.
➢ Default, The code is only accessible in the same package.
20. Non-Access Modifiers :-
• Non-Access Modifiers, do not control access level, but provides other
functionality
➢ Static, attributes and methods belongs to the class, rather than an object.
➢ Final, attributes and methods cannot be overridden/modified.
➢ Abstract, can only be used in an abstract class and methods
➢ Transient, attributes and methods are skipped when serializing the object containing
them.
➢ Synchronized, methods can only be accessed by one thread at a time.
➢ Volatile, the value of an attribute is not cached thread-locally, and is always read from
the "main memory"
21. Non-Access Modifiers :-
• Final Ex:
public class MyClass {
final double PI = 3.14;
public static void main(String[] args) {
MyClass myObj = new MyClass();
myObj.PI = 25; // error
//Scanner in = new Scanner(System.in);
//myObj.PI = in.nextLine();
}
}
22. Non-Access Modifiers :-
• Static Ex:
static void myStaticMethod() {
System.out.println("Static methods called without creating objects");
}
public void myPublicMethod() {
System.out.println("Public methods called by creating objects");
}
23. Encapsulation :-
• Encapsulation, is a programming technique that binds the class members
(variables and methods) together and prevents them from being accessed
by other classes.
Variables
Methods
Class
{
variables
+
Methods
}
class
Encapsulation
Encapsulation
24. Encapsulation :-
EX :
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}