You are on page 1of 14

SKILLS FOR INDIA

JAVA

1
Document Version: 1.00

Session 3
GUI Application Development using Java8
Topics to be covered in this session:

Class in Java

2
Document Version: 1.00

Key Learning Objectives


Illustrate that a java class is used to implement abstraction
List all the components a class can contains
Define a class, fields and methods
Demonstrate naming convention for class, method and field

3
Document Version: 1.00

What Is A Class ?
A design or template or blueprint of all the objects of a certain kind
It defines all the variables ,methods all objects should have
It is expressed as a piece of program code
An instance of a class Object

4
Document Version: 1.00

Class Example..
class Rectangle{
private int length,width;
public int calculateArea(){
int area=length*width;
return area;
}
public int calculatePerimeter(){
int peri=2*(length+width);
return peri;
}
public void setData(int len , int wid){
length=len;
width=wid;
}

5
Document Version: 1.00

Class Example..
public class RectangleTest{
public static void main(String args[]){
Rectangle rectangle1=new Rectangle();
Rectangle rectangle2=new Rectangle();
rectangle1.setData(10,20);
rectangle2.setData(30,40);
int area=rectangle1.calculateArea();
System.out.println(Area : +area);
}
}

File name :- RectTest.java


6
Document Version: 1.00

Declaring Objects
Creating object is a two step process
Declare variable of class type; it is simply a reference to object of a
particular type
Acquire an actual, physical copy of object and assign it to that
variable (new operator allocates memory at runtime and returns
reference)
Box mybox;//null ref, if used, error
mybox = new Box();// can be combined

7
Document Version: 1.00

Scope Of A Variable
Local variable
Declared in methods
Smallest life span
Instance variable/method
Associated with object
As long as object exists , they exists
Class variable/method
Associated with the class
Highest life

8
Document Version: 1.00

Exercise
Develop a Java Program to Create a class Student get marks in
three subjects and calculate appropriate result.

9
Document Version: 1.00

Class(static) Variable/Method
Also known as static variable/method
They are not associated with object, so no object reference is necessary to
access them
They are accessed by class name
static keyword is used to declare a static variable/method
No instance variable/method can be accessed from static function

10
Document Version: 1.00

Static Example..
class Rectangle{
private int length,width;
public static int counter;
public int calculateArea(){
int area=length*width;
return area;
}
public int calculatePerimeter(){
int peri=2*(length+width);
return peri;
}
public void setData(int len,int wid){
length=len;
width=wid;
}
}

11
Document Version: 1.00

Exercise
Develop a java program to create a class to keep track of how many
objects are created and provides an unique id number (starting from 1)
sequentially for each of its objects. Display total number of objects and the
id number of each object.

12
Document Version: 1.00

Document Amendment History

13
Document Version: 1.00

T H A N K Y O U. . .

All information, including graphical representations, etc provided in this presentation is for exclusive use of current Globsyn
Skills students and faculty. No part of the document may be reproduced in any form or by any means, electronic or otherwise,
without written permission of the owner.
14
Document Version: 1.00

You might also like