Class, Object and Method in Java.

We are going to discuss classes and objects, when you talk about Java then class and object are a very important part of Java. Class and object are concepts of oops. we are going to discuss what is class and object, what are used, and where we use them.

Object and Class in java is a very power full concept without it nothing we can do in programming. Object and Class in java is a very important thing so we need to know this concept very well. 

 

 

We are also talking about methods, types of methods, method declaration, and how to call a method in Java.

What is a class in Java?

We can simply say a Class is a user-defined data type, which has a group of objects and common properties. or we can say it is a blueprint or template from which objects are created. It is a logical entity.

 

A Java class contains:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

 

 //Defining a Employee class.  
class Employee{ 
 //creating main method inside the Employee class  
 public static void main(String args[]){  
  System.out.println("Class name is Employee");  
 }  
} 

 

What is an object in Java?

We can simply say, an object is an instance of a class. It is a real-world entity like a pen, dog, table, etc. An object in Java is a physical as well as a logical entity.

An object has three characteristics.

  • Identity: An object identity is typically implemented via a unique ID.
  • State: It represents the data (Value) of an object.
  • Behavior: It represents the behavior (functionality) of an object.

 

Let’s try to understand with an example, for Example Dog is an object.
The dog’s name is Tomy, so his Identity is Tomy, Dog color is Black, color is known as its state. The dog is barking, so barking is its behavior.

 

How ways to create an object in Java?

We are creating Objects in java in many like.

  • By new keyword.
  • By newInstance() method.
  • By clone() method.
  • By deserialization.

 

How many ways to initialize objects?

There are 3 ways to initialize an object in Java.

  • By constructor
  • By method
  • By reference variable
 

What is an Anonymous object?

Anonymous means nameless. An object which has no reference is known as an anonymous object. It can be used only when object creation.

If you want to use an object only once, an anonymous object is a good approach.

 

For example:

new sum();//anonymous object 
class Addition {  
 void sum(int a, int b){  
  int sum = a + b;
  }  
 System.out.println("Sum of two number is "+sum);  
}  
public static void main(String args[]){  
 new Sum().sum(10,20);//calling method with anonymous object  
}  
}
Output:
Sum of two number is 30

 

Different between the Calling method through a reference and the Calling method through an anonymous object.

 

class Addition {  
 void sum(int a, int b){  
  int sum = a + b;
  }  
 System.out.println("Sum of two number is "+sum);  
}  
public static void main(String args[]){  
 Addition  s = new Addition ();
 s.sum(10,20); //Calling method through a reference  
 new Sum().sum(10,20);//calling method with anonymous object  
}  
}

Output:

Sum of two number is 30
Sum of two number is 30
 

Example of Class and Object

Example of Class and Object
//Defining a Employee class.  
class Employee{  
 //defining fields  
 int id;
 String name;  
 //creating main method inside the Employee class  
 public static void main(String args[]){  
  //Creating an object or instance  
  Employee e1=new Employee();//creating an object of Employee  
  //Printing values of the object  
  System.out.println(e1.id);//accessing member through reference variable  
  System.out.println(e1.name);  
 }  
} 

 

Method in Java

A method is a way to perform some tasks, it is a block of code, or in Java, it is a collection of instructions, that performs a specific task. It provides the reusability of code, which means we can write a method and use that method many times.

 

It is executed only when we call or invoke it. In Java, the most important method is the main() method. Java always starts executing java code from the main() method.

Why use methods?

The method provides the reusability of code, which means we can write a method and use that method many times.

 

How to Call a Method?

We are writing a method in java using parentheses () and a semicolon like myMethod(). It needs to be called a method for using its functionality.

 

public class User {
  static void myMethod() {
    System.out.println("Today I am learning method");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

Output:

Today I am learning method
 

Example of calling the same method multiple times.

public class User {
  static void myMethod() {
    System.out.println("Today I am learning method");
  }

  public static void main(String[] args) {
    myMethod();
	myMethod();
	myMethod();
  }
}

Output:

Today I am learning method
Today I am learning method
Today I am learning method

 

Types of Method

There are two types of methods in Java:

  • Predefined Method
  • User-defined Method

 

Predefined Method

Predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. Some pre-defined methods are sqrt(), length(), equals(), compareTo(), size() etc.

 

public class DemoPredefined   
{  
public static void main(String[] args)   
{ 
String s = "Checking Predefined method";
har[] chr = s.toCharArray();
// using the length method we are finding Characters  count of string 
System.out.print("Characters  count of string : " + s.);  
}  
}

Output:

Characters count of string : 26.

 

User-defined Method

The user-defined method is written by the user or programmer. we are writing this method according to our requirements.

 

package practice.Number;

public class OuntputChecking {
		  public static void findEvenOdd(int num)  
		{  
		//method body  
		if(num%2==0)   
		System.out.println(num+" is even");   
		else   
		System.out.println(num+" is odd");  
		}

		  public static void main(String[] args) {
		    findEvenOdd(12);
		  }

}

Output:

12 is eve

Static Method

A method that has a static keyword is known as a static method. The static method always belongs to a class. when we write the static keyword before the method name, then that method is called the Static method.

The main advantage of a static method is without creating an object we can call a static method like “public static void main(String[] args)”, the main method is always called without creating an object.

 

public class TestingStatic  
{  
public static void main(String[] args)   
{  
show();  
}  
static void show()   
{  
System.out.println("It is an example of static method.");  
}  
} 

 

Instance Method

It is a non-static method defined in the class.
The method of the class is known as an instance method. when we are calling an instance method, it is necessary to create an object of its class

 

public class InstanceMethod 
{  
public static void main(String [] args)  
{  
//Creating an object of the class  
InstanceMethod obj = new InstanceMethod();  
//calling instance method   
System.out.println("The sum is: "+obj.sum(12, 13));  
}  
int s;  
//user-defined method because we have not used static keyword  
public int sum(int a, int b)  
{  
s = a+b;  
//returning the sum  
return s;  
}  
}

 

Read more topics related to java

 

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *