0% found this document useful (0 votes)
31 views

C4learn - Understand Classes in Java

This document discusses a Java program that demonstrates the use of classes. It includes: 1. A Rectangle class that declares length and breadth instance variables. 2. A RectangleDemo class with a main method that creates a Rectangle object, assigns values to its length and breadth, and calculates its area. 3. An explanation of class concepts - how a class is declared, objects are created using the new keyword, and instance variables of an object can be accessed using the dot operator. 4. Steps to run the program by creating and saving the file, and running it from the command prompt.

Uploaded by

qabiswajit
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

C4learn - Understand Classes in Java

This document discusses a Java program that demonstrates the use of classes. It includes: 1. A Rectangle class that declares length and breadth instance variables. 2. A RectangleDemo class with a main method that creates a Rectangle object, assigns values to its length and breadth, and calculates its area. 3. An explanation of class concepts - how a class is declared, objects are created using the new keyword, and instance variables of an object can be accessed using the dot operator. 4. Steps to run the program by creating and saving the file, and running it from the command prompt.

Uploaded by

qabiswajit
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

In the Previous tutorial we have leaned basic syntax of class.

In this
tutorial we are going to learn actual programmatic implementation of
Class using Java. Firstly read below document carefully and then
follow steps given at the end of article in order too run it on your
system.
Table of content [hide]

o
o
o

1 Live Example : Simple Class Program [Area of Rectangle]


1.1 Output :
2 Explanation : Class Concept
2.1 1. Class Creation / Declaration :
2.2 2. Creation of Object
2.3 3. Accessing Instance Variables Of Object/Instance using
DOT Operator
3 Steps to Run Above Program

Live Example : Simple Class Program [Area of


Rectangle]
class Rectangle {
double length;
double breadth;
}
// This class declares an object of type Rectangle.
class RectangleDemo {
public static void main(String args[]) {
Rectangle myrect = new Rectangle();
double area;
// assign values to myrect's instance variables
myrect.length = 10;
myrect.breadth = 20;
// Compute Area of Rectangle
area = myrect.length * myrect.breadth ;
System.out.println("Area is " + area);
}
}

Output :
C:Priteshjava>java RectangleDemo
Area is 200.0

[336280]

Explanation : Class Concept


1. Class Creation / Declaration :
Consider following class
class Rectangle {
double length;
double breadth;
}

class defines a new type of data.


Rectangle is our new data type.
Rectangle will be used to declare objects of type
Rectangle.
class declaration only creates a template. It does not create an
actual object.

2. Creation of Object
Rectangle myrect = new Rectangle();

Actual Creation of Object.


Memory is allocated for an object after executing this
statement.
This Statement will create instance of the class Ractangle and
name of instance is nothing but actual object myrect.
Fresh copy of Instance variables gets created for Fresh
Instance. [myrect now have its own instance variables ->
length,breadth ]
Following fig shows one unknown term Constructor (Dont
worry about this term , you will get more details in the incoming
chapters)

3. Accessing Instance Variables Of Object/Instance using


DOT Operator
myrect.length = 10;
myrect.breadth = 20;

As explained earlier , Each Instance/Object gets their own copy


of instance variables i.e length and breadth.

We can access myrects copy of instance variable using


DOT operator (as shown above).
[46860]

Steps to Run Above Program


1. Create RectangleDemo.java . [suppose your file have
multiple classes then you must save file with class name
that contain main class ]
2. Copy Above Program into RectangleDemo.java and save it.

3. Run MyRectangle.java inside Command Prompt.

You might also like