class and object,cosntructor, block notes
class and object,cosntructor, block notes
***object characteristics
CLASS => group of objects which have common properties. it is logical entity
A class contain
1. field/instance var => the data/var defined inside the class but outside
method is called as instance varibles/fields/data members of class
2. methods => class needs to have to operate on the data or an
instance var
3. constructor
4. block
5. nested class and interface
Syntax of Class:
instance method1;
instance method2;
.
.
.
};
2. instantiate object or assign the object refrence to the variable we can use
new operator to dynamically allocate memory for an object
return a+b;
}
2.method name => specifies the name of method.It is an any valid java
identifier(user defined names)
3.parameter list => A method that accepts parameters must list the
parameters listin the method declaration.
The parameters are placed in a parameter list inside
the parentheses that follow the method name.
For each parameter used by the method, you list the
parameter type followed by the parameter name.
costructor characteristics
1. It performs automatic initialization of object
2. constructor name should be same as class name
3. constructor has no return type, not even void
types of cons:
i. default constructor
the constructor which does not have any arg/parameter.
- class_name( )
{
//body of cons
}
- It is similar to the method which has no name. It can written inside the class
but not inside the method
- It is always executed before the costructor.
- we can use variables only inside instance initializer block not method
- Its compulsory to create the object of the class if you use instance
initializer block in your class
Syntax:
{
//initializes instances var/data member
}