Vous êtes sur la page 1sur 2

-1- subhashv1224@gmail.

com
Class : this is a template from which you can create objects. The definition of class includes the formal
specifications for the class and any data and method in it.

Object: this is an instance of a class much as a variable is an instance of a data type. We can think of a
class as the type of an object. Objects encapsulates methods and instance variables.

Data members: those variables that are part of a class. We use them to store the data the object uses.
Objects supports both ‘instance variables’ whose values are specific to the object and ‘class/static
variables’ whose values are shared among the objects of a specified class.

Methods/Functions: this is a function built into a class or object. We have instance and class methods.
We can use instance methods with objects and class methods just by referring to the class name, no
object is required.

Class declaration and definition syntax:

[access] class class_name [extends …] [implements…]

{
// class definition

[access] [static] type variable1;


[access] [static] type variable2;
.
.
.
[access] [static] type variableN;

[access] [static] return-type method1([parameter-list])


{
(method definition)
…..
}

[access] [static] return-type method2([parameter-list])


{
(method definition)
…..
}
.
.
.
[access] [static] return-type methodN([parameter-list])
{
(method definition)
…..
}

} // end class definition

The keyword ‘static’ turns variable into a class variable and a methods into a class method.
The ‘access’ term specifies the accessibility of the class or a class method or a class variable to bthe rest
of the program and it can be public, private or protected.

Instance and Class variables: instance variables are specific to the objects. If you have 2 objects(i.e. two
instances of a class), the instance variables in each object are independent of the instance variables in
other object
On the other hand, class variables of both the objects will refer to the same data and therefore will hgold
the same value.

S.o.p. – System.out.println
-2- subhashv1224@gmail.com
Ex.
public class A {
int a; // instance variable
static int b; //class variable
……
} // end class A

class try {
public static void main(String[] args)
{
Output:
A a1=new A(); // object a1 of class A
A a2=new A(); // object a2 of class A Value of a1.a=10
Value of a2.a=0
a1.a=10; // a1 accessing instance variable a Value of a1.b=100
S.o.p(“Value of a1.a=” + a1.a); Value of a2.b=100
S.o.p(“Value of a2.a=” + a2.a);

a1.b=100; // a1 accessing class variable b


S.o.p(“Value of a1.b=” + a1.b);
S.o.p(“Value of a2.b=” + a2.b);
} // end main
}// end class try

In the above example, instance variable ‘a’ is different for both the objects. Thus changes made to ‘a’ by
the object a1 is not reflected in object a2.
In the above example, the class variable ‘b’ is same for both the objects or say both the objects share the
same class variable ‘b’. thus changes made to ‘b’ by object a1 is reflected in object a2.

To declare and define a method, you can:


 Use access specifier
 Specify the return type of the method if you want to return a value(such as int, float,an object type or
void if the method doesn’t return anything.
 Give the method’s name and
 Place the list of the parameters you intend to pass to the method.

An instance method can only be invoked by an object of a class while class method can be invoked directly
by a class.

Ex.
class A {

void msg() {
S.o.p(“Helloi”);
}
static void msg2(){
S.o.p(“Hello World”);
}
} // end class

class try {
public static void main(String[] args){
A a1=new A();
A a2=new A();

a1.msg(); // object a1 calling instance method


a2.msg(); // object a2 calling instance method

A.msg2(); // class A invoking static method


A.msg(); // illegal as class can’t invoke instance method directly
} // end main
} // end class

?? try to figure out its output

S.o.p. – System.out.println

Vous aimerez peut-être aussi