Professional Documents
Culture Documents
Programming Concepts in
Java
4
Class
Class contains two
things:
Data (variables)
Code (methods)
Collectively, the
methods and variables
defined within a class
are called members of
the class.
5
Example: Creating a Class
6
Creating Object
classname class-var;
7
Example: Creating an Object
8
Example: Creating an Object in
Other Class
9
Method
General form of a method
Methods that have a return type other than
void, return a value to the calling routine
using the following form of the return
statement:
10
Example: Method with return
value
Output:
12
Constructor
In Java, a constructor is a block of codes similar
to the method.
It is called when an instance of the class is
created. At the time of calling constructor,
memory for the object is allocated.
If there is no constructor available in the class,
Java compiler provides a default no-arg
constructor.
There are two types of constructors in Java: no-
arg constructor, and parameterized constructor.
13
Constructor
14