2. Class
• Java class is nothing but a template for object you are going to create or it’s a blue
print by using this we create an object.
Class (name of the class)
{
(Here define member of class)
}
• A class is a user defined datatype with a template that serves to define its properties
• Members of a Class
• Field: instance variables are called fields
• Method: method is nothing but the operation that an object can
perform
Class Stock
{
/*fields */
Public commodity;
Public price;
/*method /
ublic void buy (int no_of_commodity) {}
ublic boolean sale () {}
}
3. Variables and method declaration
• Data is encapsulated in class by placing variables inside the body of the
class
class Demo{
int int_variable,a;
}
• A method is a set of code which is referred to by name and can be called
(invoked) at any point in a program
• Method declaration has four basic parts
• Method name,Return type,Argument list,Method body
class Demo
{
type method-name(parameter list)
{
method body;
}
}
5. Object
• "object" refers to a particular instance of a class where the object can be
a combination of variables, functions, and data structures and block of
Memory to store all variables,fuctions,data structure.
• Creating an Object :Objects in java are created using the new operator.
The new operator creates the objects of the specified class and return the
referenc to that object
class_name <object_name>=new class_name();
class Demo_class
{
int int_variable;
public void demo()
{
System.out.println("hello");
}
public static void main(String[] y)
{
/*
*creating and object
*/
Demo_class a=new Demo_class();
System.out.println("what contains in a object "+a);
}
}
6. Accessing instance variables and
methods
class Demo_class
{
int int_variable=10;
public void demo()
{
System.out.println("hello");
}
public static void main(String[] y)
{
/*
*creating and object
*/
Demo_class a=new Demo_class();
a.demo();
System.out.println("the value of int_variable is"+a.int_variable);
System.out.println("what contains in a object "+a);
}
}
7. Assigning Object reference variables
• Object reference is the variable that holds the memory location of the real
object. Object Reference variable is just like pointer in c but not exactly
class_name <object_name>=new class_name();
class_name <refference_name>=object_name;
class Demo_class
{
int int_variable=10;
public void demo()
{
System.out.println("hello");
}
public static void main(String[] y)
{
/*
*creating and object
*/
Demo_class a=new Demo_class();
Demo_class b=a;
b.int_variable=20;
System.out.println("the value of int_variable is"+a.int_variable);
}
}
8. Methods
• A method is a group of Java statements that perform some operation on
some data, and may or may not return a result.
class Demo_class
{ public void demo()
{System.out.println("hello");}
/* method with return type */
public int demo_1()
{return (2);}
/*method with arguments*/
public void demo_2(int a)
{ System.out.println("the parameter a value is::"+a);}
public static void main(String[] y)
{/* *creating and object */
Demo_class a=new Demo_class();
/*method calling */
a.demo();
System.out.println("the return type value is "+a.demo_1());
a.demo_2(10);
}
}
9. Constructor
• When ever we are creating an object some piece of code will execute
internally to perform initialization, that piece of code is nothing but
“constructor“.
Rules for writing constructors
• The name of the class and name of the constructor must be same
• A constructor may have or may not have parameters
• If a constructor doesn’t have any parameters, it is called ‘default‘ constructor.
• If a constructor has 1 or more parameters, It is called “Parameterized constructor.
• Return type is not allowed for the constructors even void also
• the only applicable modifiers for the constructors are private, protected, default, public. If we
are using any other modifiers we will get compile time error.
• A constructor is automatically called and executed at the time of creating an object.
• If nothing is passed to the object,then default constructor is called and executed.if some
values are passed to the object ,then the parameterized constructor is called
• A constructor is called and executed only once per object
•
11. This Keyword
• Which can be used inside method or constructor of class
• It(this) works as a reference to current object whose method
or constructor is being invoked
• this keyword can be used to refer any member of current object from
within an instance method or a constructor.
Usage of java this keyword
• this keyword can be used to refer current class instance variable.
• this() can be used to invoke current class constructor.
• this keyword can be used to invoke current class method (implicitly)
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor call.
• this keyword can also be used to return the current class instance.
12. Instance variable hiding with this key
word
class Demo_class
{
int i,j=20;
public void demo(int i)
{
this.i=i;
System.out.println("the value i in method"+i);
}
public static void main(String[] y)
{
Demo_class d=new Demo_class();
d.demo(30);
System.out.println("the class variable i::"+d.i);
}
}
13. Garbage Collection
• Java destruction of object from memory is done automatically by the JVM.
When there is no reference to an object, then that object is assumed to be
no longer needed and the memory occupied by the object are released.
This technique is called Garbage Collection. This is accomplished by the
JVM.
Advantage of Garbage Collection
• It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
• It is automatically done by the garbage collector(a part of JVM) so we
don't need to make extra efforts.
14. • Correct the error in the following code
Class Variable_Name
{
Public static void main(string[] y)
{
Sytem.out.println(“hello world”);
}
};
Correct the Error in the Following Code
Class Return_value
{
public string name=“team”;
public string getName()
{
System.out.println(“hello world”);
return(“hello”);
}
public static void main(String[] y)
{
Return_value rv=new Return_Value();
String y;
y=rv.getName();
System.out.println(“”+y);
}
}