Font Awesome Icons

Tutorial

object and class in Java

object:

An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tangible and intangible). The example of the intangible object is the banking system.

An object has three characteristics: 

state: represents data (value) of an object.

behavior represents the behavior (functionality) of an object such as deposit, withdraw etc.

identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.

 

Class:

A class is an entity that determines how an object will behave and what the object will contain.

In other words, it is a blueprint or a set of instruction to build a specific type of object.

 


A class in Java can contain:


  • fields

    methods

    constructors

    blocks

    Syntax

 

Object and Class Example1:

 

class Animal
{
int weight;                    // instance variables
String name;
public static void main(String args[])
{
Animal a1=new Animal ();<—–object created
System.out.println(a1.weight);
System.out.println(a1.name);
}
}

 

The new keyword is used to allocate memory at run time. All objects get memory in Heap memory area. Here a1 is the reference variable which is used to access instance variables. The reference variable is also used to call methods. The need for object creation is there because the instance variables are created outside the main method.

Object and Class Example2:

 

class Lion{
int weight;
String name;
}
public class Animal

{
public static void main(String args[])

{
Lion a1=new Lion();
System.out.println(a1.weight);
System.out.println(a1.name);
}
}

Method:

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.

 

Each method has its own name.  When that name is encountered in a program, the execution of the program branches to the body of that method.  When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.

 

Method Example1:

 

class Animal{
void eat()
{
System.out.println(“eating”);
}
}
class Main{
public static void main(String args[]){
Animal a =new Animal();
a.eat();
}
}

Method Example2:

A method can be called multiple times.

 

  class Animal{
void eat()
{
System.out.println(“eating”);
}
}
class Main{
public static void main(String args[]){
Animal a =new Animal();
a.eat();

a.eat();
}
}

Method Example3:

A method can have parameters as well. Parameters are specified after the method name, inside the parentheses. The parameters in the method must match the arguments given.

 

public class Sum {
public void sum(int x, int y)
{
System.out.println(x+y);
}

public static void main(String args[]) {
Sum s = new Sum();
s.sum(5, 7);

}
}

Method Example4:

If we want the method to return a value, we can use a primitive data type (such as int, char, float, double etc.) instead of void, and use the return keyword inside the method body.

public class Sum
{
public int sum(int x, int y)
{
return(x+y);
}

public static void main(String args[]) {
Sum s = new Sum();
System.out.println(s.sum(5, 7));

}
}

 

Method Example5:

In this example, two separate methods are used one to accept the data and others for showing the data.

 

class Leo

{
int weight;
String name;
void addinfo(int w, String n){
weight=w;
name=n;
}
void displayinfo()
{
System.out.println(weight+” “+name);}
}
public class Animal2{
public static void main(String args[]){
Leo s1=new Leo();
Leo s2=new Leo();
s1.addinfo(111,”liger”);
s2.addinfo(222,”puma”);
s1.displayinfo();
s2.displayinfo();
}
}

Hi, Welcome to etechvidya