Class and Object in Java with Programs and Examples

Class and objects

Why do we need a class in Java?

Java is an object-oriented programming language and represents data in the form of Objects. So in order to create an object, we required a class.

What is a class?

A class is user-defined data type in Java & is a Structure representing data and logic.
                         (or)
A class is a specification or blueprint or template of an object. A class defines the structure, state and behavior mean data & code that can be shared by a set of objects.
The data defined by the class are referred to as member variables or instance variables or attributes.

Syntax to declare a class:

Class<class name>
{
Type instance variable 1;
Type instance variable 2;
…..……………………………….
 Type instance variable N;

Type  methodname1(parameter list)
{
 //body of method//
 }
Type methodname2(parameter list)
 {
    //body of method//
  }
……………………………………
Type methodnameN(parameter list)
 {
    //body of method//
  }
}

Declaring a variable: In Java, all variables must be declared they can be used. 
Variable declaration is shown below:
Type identifier [ = value], identifier [ = value], ………;

Example:

int a, b, c;       // declares three integers a, b, c;
int i = 1, j = 2, k;   //declares three more integers & initializing i & j;

Class Example:
Class A
{
int i,j;
 Void funA()
{
System.out.println(“FunA() of class A”);
}
}

Understanding above program
1) Where “A” is name of the class.
2) “i& j” are instance variables.
3) “funA()” is the name of the method (or) function.

Q) What is Next?
a) Object.

Why do we need an Object?
In order to load the members of the class(means instance variables, methods) from the byte code to the Ram in the form of executable format and make them available to the program which is under execution. Without using the concept of an object, we can’t load the members of the class from the byte code to the RAM at the runtime.

What is an object?
An object is an instance of a class. The memory space which is allocated for the members of a class, dynamically at the runtime can be called as an object. 


What is an Instance?

Any dynamic memory allocation can be called as an Instance. That is memory space allocated for anything during runtime can be called as an Instance.

Syntax to declare Object:

<Class Name>   ClassObjectReference = new <Class Name>();

Object Example for Above program


A a1 = new A();


Ways to create Object: There are various ways to create an Object. The most commonly used and the simple  and easiest way to create Object is using New keyword;

Example:

class Objectexample
{
 public static void main(String[] args)
 {
  Objectexample e = new Objectexample();
  System.out.println("Example of  Object using th new keyword");
  System.out.println("E="+e);
 }
}

Output:
Example of  Object using the new keyword
E = Objectexample@42e922

Types of Objects In Java:
In Java, we have two types of objects base on the referenced variables. They are:
     1)    Referenced (or) Reachable Object.
     2)    In-referenced (or) Un-reachable Object.



Referenced (or) Reachable Object: If an object is pointed by at least one referenced variable, it is called Referenced (or) Reachable object.

Example of Referenced Objetc creation :
ObjectExample e = new ObjectExample();

Program of Referenced (or) Reachable Object:

class Objectexample
{
 public static void main(String[] args)
 {
  Objectexample e = new Objectexample();
  System.out.println("Example of Referenced Object");
  System.out.println("E="+e);
 }
}

Output:

Example of Referenced Object
E = Objectexample@52e922

Un-Referenced (or) Un-Reachable Object: If an object is not pointed by at least one referenced variable, it is called Un-Referenced (or) Un-Reachable object.

Example of Un-Referenced Object creation: new Object Example();

Program of Un-Referenced (or) Un-Reachable Object:

class Objectexample1
{
 public static void main(String[] args)
 {
  new Objectexample1();
  System.out.println("Example of Un-Referenced Object");
  System.out.println("E="+ new Objectexample1());
 }
}

Output:
Example of Un-Referenced Object
E = Objectexample1@52e922


The Five different locations where we can define Objects in Java are:

1) Defining Object has local to a function.
2) Defining Object has an instance variable (or) Non-static member of a class.
3) Defining Object has the parameter of the function.
4) Defining Object has a return data type of the function.
5) Defining Object has a Static variable of the class.

Defining Object has local to a function:
class Demo1
{
int x,y;
void fun1()
{
 x = x + 1;
A a1 = new A();
a1.i = x + 1;
 }
 public static void main(String[] args)
{
A a1 = new A();
Demo1 d1 = new Demo1();
d1.x = 2;
 System.out.println("a1.i = "+ a1.i);
System.out.println("d1.x = "+ d1.x);
 }
}

Output:
a1.i = 0; 
a1.x = 2; 

Defining Object has an instance variable (or) Non-static member of a class.


class Demo2
{
 int x = 1;
A a1 = new A();
public static void main(String[] args)
{
Demo2 d1 = new Demo2();
System.out.println(d1.a1);
System.out.println(d1.a1.i);
d1.a1.funA();
}
}

Output:

A@522e34
0
funA() of A

Defining Object has a parameter of the function.

class Demo3
{
 void fun1(int x)
{
int sq = x*x;
System.out.println("sq = " +sq);
 }
public static void main(String[] args)
{
  Demo3 d1 = new Demo3();
  d1.fun1(5);
   int x = 7;
  d1.fun1(x);
 }
}

Output:

sq = 25
sq = 49

Defining Object has return data type of the function:

class Demo4
{
int getSq(int x)
{
int sq = x * x;
return(sq);
 }
 public static void main(String[] args)
{
 Demo4 d1 = new Demo4();
  int x = d1.getSq(2);
 System.out.println("X = " +x);
 System.out.println(d1.getSq(4));
 int y = d1.getSq(5) + 2;
 System.out.println("Y = " +y);
 }
 }

Output:

x = 4 
 16 
y = 27 

Defining Object has a Static variable of the class.

Program 1:
class Demo5
{
 static int x = 1;
static A a1 = new A();
}

Program 2:
class Demo6
{
public static void main(String[] args)
{
System.out.println(Demo5.x);
System.out.println(Demo5.a1);
System.out.println(Demo5.a1.i);
Demo5.a1.funA();
}
}

Output:

1
A@527e33  
0
funA() of A
Also Read: Java strings interview questions Java collections interview questions Java exception handling interview questions Java multithreading interview questions Java io streams interview questions Core Java interview questions Java constructor interview questions USPS Tracking USPS Hold Mail   Liteblue Liteblue login Earthlink Webmail  RCN Webmail BU Webmail Towson University Webmail Gmail login Shaw Webmail Cox Email GoDaddy Webmail Walmart Credit Card Login TCS Webmail bookzz 192.168.0.1 liteblue usps gov human resources Hotmail login WalmartOne Login upsers outlook webmail Factorial program in Java constructor in java switch case in java java data types java if else
Share:

Search This Blog

  • ()

Our Services

Powered by Blogger.

Top 20 Websites like Bookzz | Bookzz Alternatives Websites

The domain  Bookzz.org  isn’t into work anymore, and the world’s largest ebook library  Bookzz  has got a new domain name or its Alternati...