Você está na página 1de 5

Modifiers

Features of Java
1 OOL Object Oriented Language
2 platform Independance
3. Architectural neutral Independent of OS
4. JVM
5. ByteCode intermediate code JVM can understand
6. PAcakages is dierectory in file system and collection of class.
7. interfaces; purely abstract nothing defined in interface.
8. JNI - To access system resources
9. Exception Handling
10. MultiThreading
11. Two Types of Program
A. Application Program or Stand alone program always starts at paticular
point hich is Main function
B. Applet Program
When we compile java file we get class file, class file consists of ByteCode.
JVM can understand only ByteCode.
Java is multilevel single inhertance.
A
B E
C
D
we cannot have A and B both being parent of C
A B
C not possible.
Data types
Character
Floating
char
(2)
float

(4)

double

(8)

Integer
Boolean
short (2)
boolean (1) value true/false
int
long
byte

(4)

(8)
(1)

Arrays
Arrays are object in java
ex
int num[] = new int[5];
A arr[] = new A[5]; intial value of array is null; starts from zero
A arr[][] = new A[5][] each row can have different length her 5 rows
arrr[0][3] = { new A(), new A(10),new A(20,'xx') }
Modifiers
abstract
static
final
native
transient
synchronized
access Specifiers.
access Specifiers

to specify scope of members of a class and the class itself


available access specifiers are public private protect
when you are not specifying then it will called as default scope or packet scop
e
Private access specifiers can be given to any member of class including constru
ctor but class cannot have
when it is private we can access within the class only not outside.
protect access specifiers n be given to any member of class including construct
or but class cannot have
when it is protect we can access within the class and subclass/child class of s
ame package
and subclass/child class of different package and non subclass in a same packag
e..
Default/Packet Scope
when it is Default we can access within the class and subclass/child class of
same package
and non subclass in a same package.
Public access specifiers can be access from any where.
class can be public or default specifier. Class cannot have private to protecte
d.
package p1;
public class A
{
int i;
private char ch;
public float f;
protected double d;
void dsp()
{
i, ch, f, d can be access
}
}
public class B extends A
{
d, i, f
}
public class C
{
d need to have instance
i need to have instance
f need to have instance
}
---------------------package p2;
public class D extends A
{
d, f
}
public class E

{
f need to have instance
}

NAtive Modifiers
NAtive modifier used in JNI concept
public class A
{
public native void f1(); declare only and no defnition. Definition will be in o
ther language like C or C++
}
abstract modifier
public class A
{
public abstract void f1(); declare only and no defnition. Definition will be su
b/child class in java
}
static modifier
static can be give any member of the class but not class itself.
static means global for all the objects.
static memory allocation will be in method allocation area.
instance variable non static will be allocated in heap
static variable get only once when the class is loaded into jvm in the method a
rea;
similarly static method which can be used globaly across all the objects.
static members are accessed through class names itself. A.disp();
you can also access use object A a1 = new A(); a1.disp(); internal java will ch
eck the access specifier
and since it is static it will access through class.
all non static members are accessed through object variables. A a1 = new A(); a
1.i;
there two types of members in a class
instance member any non static variable in a instance is instance member
class member any static variable in a instance is class member
similarly methods instance methods and class methods
you cannot/donot access the instance member or instance variable directly in st
atic method.
you have to create instance of the object and then you have to access
or you have to pass the instance object and the access the member through that
object
in instance method you can access both static and non-static members.
public class A
{
static double pi;
int A;
char ch;

Void set() { }
void get() {}
static void disp() {}
}
Instance block and static block
you can have any number of Instance block and static block any where in the cla
ss.
Static block written using the keyword static
static {
}
instance block no keyword
{
}
static block executed when it is loaded into jvm after creating static variable
s then static block is executed only once for a class.
instance block executed every time when instance of a class is created and bef
ore constructor is called
since you can have any number of static and instance block the sequence of exec
ution is sequence of appearance in the class

constructor can be private and protected


singleton pattern means for a class at any point you can create only one instan
ce. cannot create from outside.
class A
{
static A obj;
private A()
{}
public static A getA(){
if (obj == null )
{
obj = new A();
}
return obj;
}
}
Final modifier
USed in class variable and method.
When variable is specified as a final and assign a value when defining or insi
de the constructor
after that you cannot change the value.

When method is specified as a final and when you derive/extend/inherit the cl


ass then
in the child class you cannot override the method.
when class is defined as a final then you cannot have a subclass or you cannot
derive/extend/inherit
fianl class A
{
final int i;
final void get() { };
}

Você também pode gostar