Shaik Lalu Basha
2 min readJan 15, 2022

--

Class and Object

A class is a way of binding the data and methods together into a single unit

Without a class there is no Java program.

In object oriented programming’s, generally two types of methods.

· Member methods — are which comes under the scope of class (Java supports only member methods)

· Non-Member methods — are those which are not comes under the scope of the class (Jave does not support non-member methods)

Class Diagram:

Class Name

Variables or Fields or Properties

Methods or Accessories

Syntax :

class <classname>

{

Field declaration /initialization;

Method definition;

}

· Class is a keyword which is used for developing the user defined datatypes.

· Class name represents a valid variable name, it is used for creating the objects.

· Field declaration represents what type of data members are used as a part of class.

· Method definition is basically to perform an operation.

Example:

public class ClassDemo {

int var;

float value;

void add(){

}

Public static void main(String[] args){

// TODO Auto-generated method stub

}

}

Note:

· Memory is allocated for data members at the run time in heap memory (Dynamic memory)

· Memory is allocated for methods on stack memory whenever we call the methods.

· Memory is allocated for constants in associative memory.

· Class definition exists only one time but objects can exist multiple number of times.

Objects have states and behavior, represented by instance variables and methods.

Example:

Dog have a name “Tommy” and weighs “8kgs” and it “make noise”.

Dog

Name

Weight

makeNoise()

bark()

Name and weight represent state (instance variables)

Making noise and barking represents behavior (methods)

Program:

package com.vedha.demo;

public class Dog {

String name;

int weight;

public void makeNoise()

{

System.out.println(“Bow..Bow..”);

}

public void bark()

{

System.out.println(“bark”);

}

public static void main(String[] args){

Dog dog = new Dog();

dog.name = “Jimmy”;

dog.weight = 6;

dog.makeNoise();

dog.bark();

}

}

Note:

1. Object has behavior that acts on its states

2. In order to store the data for the data members of the class, we must create an object.

3. Instance is a mechanism of allocating sufficient amount of memory for the data members of a class is called object.

4. Blueprint of a class is known as object.

5. Value form of a class is known as object.

In Java, it follows dynamic memory allocation but not static.

In order to create memory space in Java, we must use an operator called “new” which allocates memory in heap.

Memory Allocation: Static Memory and Dynamic Memory.

Data members / Attributes / Properties à Heap Memory

Methods / Behavior à Stack Memory

Creating / defining an object:

Syntax 1:

<Classname> objectname = new <Classname ()>;

Classname() ********** Constructor

New ********** Dynamic memory allocation operator

Syntax 2:

<Classname> objectname; //object declaration

objectname = new <Classname()>; // object reference

. Initially when an object is declared, it will have a default value null.

. Here, no memory space is allocated for the data members of the class.

. When the object is referenced, the value of the object is not null and the memory is allocated for the data memory of the class.

--

--