Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
2Activity
×
0 of .
Results for:
No results containing your search query
P. 1
Java Classes and Objects

Java Classes and Objects

Ratings: (0)|Views: 179|Likes:
Published by Menisa Mendoza

More info:

Published by: Menisa Mendoza on Jan 26, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, DOCX, TXT or read online from Scribd
See More
See less

06/07/2012

pdf

text

original

 
Java Classes and Objects
 
Introduction to Java ClassesA class is nothing but a blueprint or a template for creating different objects which defines itsproperties and behaviors. Java class objects exhibit the properties and behaviors defined byits class. A class can contain fields and methods to describe the behavior of an object.Methods are nothing but members of a class that provide a service for an object or performsome business logic. Java fields and member functions names are case sensitive. Currentstates of a class¶s corresponding object are stored in the object¶s instancevariables. Methods define the operations that can be performed in java programming.A
class
has the following general syntax:
<class modifiers>class<class name><extends clause> <implements clause>
{
 
// Dealing with Classes (Class body)
 <field declarations (Static and Non-Static)><method declarations (Static and Non-Static)><Inner class declarations><nested interface declarations><constructor declarations><Static initializer blocks>
}
 
Below is an example showing the Objects and Classes of the Cube class that defines 3 fieldsnamely length, breadth and height. Also the class contains a member function getVolume().
 
 
pu
blic class C
u
be {int length;int breadth;int height;
pu
blic int getVol
um
e() {ret
u
rn (length * breadth * height);}}
 
How do you reference a data member/function? 
 
 
T
his is accomplished by stating the name of the object reference, followed by a period (dot),followed by the name of the member inside the object.( objectReference.member ). You call a method for an object by naming the object followedby a period (dot), followed by the name of the method and its argument list, like this:objectName.methodName(arg1, arg2, arg3).
or example:
 
cubeObject.length = 4;cubeObject.breadth = 4;cubeObject.height = 4;cubeObject.getvolume()
C
lass
V
ariables
± St
a
t
ic
F
ields
 
We use class variables also know as Static fields when we want to share characteristicsacross all objects within a class. When you declare a field to be static, only a single instanceof the associated variable is created common to all the objects of that class. Hence when oneobject changes the value of a class variable, it affects all objects of the class. We can accessa class variable by using the name of the class, and not necessarily using a reference to anindividual object within the class. Static variables can be accessed even though no objects of that class exist. It is declared using static keyword.
C
lass
M
e
tho
ds
± St
a
t
ic
M
e
tho
ds
 
Class methods, similar to Class variables can be invoked without having an instance of theclass. Class methods are often used to provide global functions for Java programs. Forexample, methods in the java.lang.Math package are class methods. You cannot call non-static methods from inside a static method.
In
s
t
a
n
ce
V
ariables
 
Instance variables stores the state of the object. Each class would have its own copy of thevariable. Every object has a state that is determined by the values stored in the object. Anobject is said to have changed its state when one or more data values stored in the objecthave been modified. When an object responds to a message, it will usually perform anaction, change its state etc. An object that has the ability to store values is often said tohave persistence.
onsider this simple Java program showing the use of static fields and static methods
 
 
// Class and Object initialization showing the Object Oriented conce
p
ts in Javaclass C
u
be {int length = 10;int breadth = 10;int height = 10;
pu
blic static int n
um
OfC
u
bes = 0; // static variable
pu
blic static int getNoOfC
u
bes() { //static
m
ethodret
u
rn n
um
OfC
u
bes;}
pu
blic C
u
be() {n
um
OfC
u
bes++; //}}
pu
blic class C
u
beStaticTest {
pu
blic static void
m
ain(String args[]) {Syste
m.
o
u
t
.p
rintln("N
um
ber of C
u
be objects = " + C
u
be
.
n
um
OfC
u
bes);Syste
m.
o
u
t
.p
rintln("N
um
ber of C
u
be objects = "+ C
u
be
.
getNoOfC
u
bes());}}
OutputNumber of Cube objects = 0Number of Cube objects = 0
F
i
n
al
V
ariable,
M
e
tho
ds
 
a
n
d
C
lasses
 
In Java we can mark fields, methods and classes as final. Once marked as final, these itemscannot be changed.Variables defined in an interface are implicitly final. You can¶t change value of a final variable(is a constant). A final class can¶t be extended i.e., final class may not be subclassed.
T
his isdone for security reasons with basic classes like String and Integer. It also allows thecompiler to make some optimizations, and makes thread safety a little easier to achieve. Afinal method can¶t be overridden when its class is inherited. Any attempt to override or hidea final method will result in a compiler error.
Int
r
o
duc
t
i
on to J
ava
O
bjec
t
s
 
T
he Object Class is the super class for all classes in Java.Some of the object class methods are

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->