You are on page 1of 9

Java Class

#javatheeasyway
Learning java made easy.

/chauhansumitdev
Must Know
What you write

.java .java

Initially there was


a single file.

java compiler

What gets executed .java .class

.class
.class file contains the Now there are two files with
byte code of the compiled .java file different formats.
which gets executed by the JVM(java
Virtual Machine).

JVM??
Java Virtual Machine is a virtual machine that provides runtime
environment for java programs i.e makes a system run java
programs (byte code).

/chauhansumitdev
What is a Class?
..is probably a class.

A java class is a template from which objects can be created. In simple words
it is an object factory.

Example:

Consider a classroom with many


chairs, and a white-board....
the things written on the board java class
are mere text-scripts but what a
student grasps and experiments
out from the learnings differs
from student to student in the
java objects
same classroom. (more on objects in
So here the white-board acts as a
A B C D further pages)
java class and every student a
different object.

/chauhansumitdev
Another Example:
Let's consider another example,
a stamp is just a one piece
material capable of producing any
stamp designs according to the
pattern inserted.
So,
The stamp acts as a java class and
the different patterns that can be
generated from the single device
are all objects.

Stamp = java class

Patterns = Java Objects

A class in java does not exists or has no practical significance as being a template for
objects, what actually gets allocated in the memory are the objects it creates.

/chauhansumitdev
Syntax Of Class In Java:

name of the class


class keyword for declaring a class

class classname{

//instance variables
instance variables are those variables

which are declared just below
the class name.
//methods More at the bottom of the page

}
more on methods on upcoming posts :)

Why is there need for instance variables??


As seen in above examples, for every new object created, new set of values need to be
stored. This is achieved by instance variables.

The methods and the instance variables are called the members of the class.

/chauhansumitdev
Practical Application
Let's Code A Class:
Make a Cylinder Class taking its radius and height as instance variables.

class Cylinder{ radius


double radius;
height
double height;

} This class is just a template like the stamp


discussed before, it has no significance
unless its used to make objects out of it.

For making objects we use the following syntax:


pair of paranthesis

the name of template class new keyword

classname objectname = new classname();

the name of object to be formed.


its just like different stamp patterns like
rectangle, square...

new keyword dynamically allocates memory at runtime of the program to create object i.e when the code gets
executed, the required memory is allocated during the execution of the program. Therefore dynamic allocation
allows the object to take as much space as required so that the memory management can be taken care of.

/chauhansumitdev
Creating object for Cylinder class: radius = 10

Cylinder A = new Cylinder();


As described earlier, every object has height = 100
its own copy of instance variable.
So, to access the instance variables we
use the (.) dot operator.
These variables
A.radius = 10; were present in the
class, but now they This object created is
A.height= 100; store some data, occupy
some memory. A similar to a pattern
created by the
stamp.

Now this object actually occupies some space in memory.


By this similar process many objects of Cylinder class can be created.

Cylinder B = new Cylinder(); Cylinder C = new Cylinder(); Cylinder D = new Cylinder();


B.radius = 5; C.radius = 3; D.radius = 7;
B.height= 50; C.height= 30; D.height= 70;

radius = 5 radius = 7
radius = 3

height = 50 height = 30 height = 70


B C D

B C
D

/chauhansumitdev
Up Next:
Methods In Java
Working more on the Cylinder class
using methods.

/chauhansumitdev
Happy Learning

/chauhansumitdev

You might also like