lecture-05 Mobile Application and Development.pdf

AleenaJamil4 0 views 16 slides Oct 29, 2025
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Mobile Application and Development


Slide Content

Mobile Application Development
LECTURE 5

Java Variable
A variable is a container which holds the value while the Java program is executed. A variable is
assigned with a data type. Variable is a name of memory location. There are three types of
variables in java: local, instance and static. It is a combination of "vary + able" which means its
value can be changed.
dataType variableName = value;
int data=10; //Here data is variable 

Types of Variables
There are three types of variables in Java:
Ølocal variable
Øinstance variable
Østatic variable

Local Variables
ØDeclared inside a method, constructor, or block.
ØOnly accessible within the method or block where they are declared.
ØThey must be initialized before use, as they have no default values.
Example:
public void display() {
int num = 10; // Local variable
System.out.println{"Number: " + num);
}

Instance Variables
ØDeclared within a class but outside any method, constructor, or block.
ØEach object of the class has its own copy of instance variables.
ØInstance variables are initialized to default values if not explicitly set (e.g., 0 for int, null for
String).
Example:
public class Person {
String name; // Instance variable
int age; // Instance variable
}

Static Variables
ØDeclared with the static keyword within a class but outside any method.
ØShared among all instances of the class, meaning all objects of the class share a single copy of
the static variable.
ØThey are often used for constants or values that should be the same across all instances.
Example:
public class Employee {
static int companyID = 12345; // Static variable
String name;
}

Methods in Java
Ø It is a collection of statements that are grouped together to perform an operation.
Ø It takes some parameters, performs some computations, and then optionally returns a value
(or object).
Ø Used to perform certain actions, and also known as function.
Why use Methods?
Ø To remove redundant (repetitive) code.
Ø To reuse code: define the code once, and use it many times.

Methods
Syntax
public class main {
public void myMethod(parameters) {
// code to be executed
}
}
Method Calling:
myMethod(arg); // Method Calling

Encapsulation in Java
Ø Encapsulation in Java is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed of several medicines.
Ø We can create a fully encapsulated class in Java by making all the data members of the class
private. Now we can use setter and getter methods to set and get the data in it.
ØIn encapsulation, the variables of a class will be hidden from other classes, and can be accessed
only through the methods of their current class. Therefore, it is also known as data hiding.

Advantages of Encapsulation in Java
ØBy providing only a setter or getter method, you can make the class read-only or write-only. In
other words, you can skip the getter or setter methods.
ØIt provides you the control over the data. Suppose you want to set the value of id which should
be greater than 100 only, you can write the logic inside the setter method. You can write the
logic not to store the negative numbers in the setter methods.
ØIt is a way to achieve data hiding in Java because other class will not be able to access the data
through the private data members.
ØThe encapsulate class is easy to test. So, it is better for unit testing.
ØThe standard IDE's are providing the facility to generate the getters and setters. So, it is easy
and fast to create an encapsulated class in Java.

Example of Encapsulation in Java
//A Java class which is a fully encapsulated class.  
//It has a private data member and getter and setter methods.  
package com.javatpoint;  
public class Student{  
//private data member  
private String name;  
//getter method for name  
public String getName(){  
return name;  
}  
//setter method for name  
public void setName(String name){  
this.name=name  
}  
}  

Keywords in java
Keywords are the words in a language that are used for some internal process or represent some
predefined actions.
These words are therefore not allowed to use as a variable, names or objects.
Doing this will result into a compile time error.
Therefore, also known as Reserve words.
Some of Keywords:
boolean, break, catch, do, double, else, extends, final, finally, float, for, if, implements, import,
long, instanceof, int, interface, new, super, package, private, protected, public, return etc.

Identifiers in Java
ØIdentifiers in Java are symbolic names used for identification.
ØThey can be a class name, variable name, method name, package name, constant name, and
more.
ØKeywords/Reserve words cannot be used as identifier.
ØIdentifiers are case-sensitive.

Example of Identifiers
In this example, we have used the
following identifiers:
1.HelloJava (Class name)
2.main (main method)
3.String (Predefined Class name)
4.args (String variables)
5.System (Predefined class)
6.out (Variable name)
7.println (method)

Importance of Identifiers
1.Program Structure: Classes, variables, methods, packages, and other things are named by
identifiers that are the fundamental building blocks of Java programs.
2.Readability and Understanding: When well-chosen identifiers improve the readability and
comprehensibility of code, developers can more easily comprehend the function and goal of
various program components.
3.Modifiability: By offering a simple method of referencing and altering program elements,
identifiers aid in code maintenance and change.
4.Communication: Identifiers allow developers to communicate with each other by sharing
details about the purpose and capabilities of certain program pieces.

Rules for Java Identifiers
There are some rules and conventions for declaring the identifiers in Java. If the identifiers are not
properly declared, we may get a compile-time error. Following are some rules and conventions for
declaring identifiers:
ØA valid identifier must have characters [A-Z] or [a-z] or numbers [0-9], and underscore (_) or a dollar
sign ($). For example, @javatpoint is not a valid identifier because it contains a special character @.
ØThere should not be any space in an identifier. For example, java tpoint is an invalid identifier.
ØAn identifier should not contain a number at the starting. For example, 123javatpoint is an invalid
identifier.
ØAn identifier should be of length 4-15 letters only. However, there is no limit on its length. But, it is
good to follow the standard conventions.
ØWe cannot use the Java reserved keywords as an identifier such as int, float, double, char, etc. For
example, int double is an invalid identifier in Java.
ØAn identifier should not be any query language keywords such as SELECT, FROM, COUNT, DELETE, etc.
Tags