How to create object & method call & method definition

How to Create Object in Java

The object is a basic building block of an OOPs language. In Java, we cannot execute any program without creating an object. There is various way to create an object in Java that we will discuss in this section, and also learn how to create an object in Java.

Java provides five ways to create an object.

  • Using new Keyword(Alread discussed)
  • Using clone() method(TBD)
  • Using newInstance() method of the Class class(TBD)
  • Using newInstance() method of the Constructor class(TBD)
  • Using Deserialization(TBD)
CreateObjectExample1.java

    public class CreateObjectExample1   
    {    
    void show()    
    {    
    System.out.println("Welcome to javaTpoint");    
    }
    void name()    
    {    
    System.out.println("Aravind");    
    }      
    public static void main(String[] args)   
    {    
    //creating an object using new keyword   
    CreateObjectExample1 obj = new CreateObjectExample1();   
    //invoking method using the object  
    obj.show(); 
    obj.name();   
    }    
    }   

output:Welcome to javaTpoint
        Aravind
    public class CreateObjectExample2  (TBD)
    {  
    //constructor of the class    
    CreateObjectExample2()    
    {    
    System.out.println("Welcome to javaTpoint");    
    }    
    public static void main(String[] args)   
    {    
    //creating an object using new keyword   
    CreateObjectExample2 obj = new CreateObjectExample2();   
    }    
    }   

method in java

Method in Java or Java Method is a collection of statements that perform some specific task and return the result to the caller. A Java method can perform some specific task without returning anything. Methods in Java allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is different from languages like C, C++, and Python. 

1. A method is like function i.e. used to expose behavior of an object.
2. it is a set of codes that perform a particular task.

Syntax: Declare a method

<access_modifier> <return_type> <method_name>( list_of_parameters(TBD))
{
    //body
}

Method Declaration

In general, method declarations has six components :  

1. Modifier: It defines the access type of the method i.e. from where it can be accessed in your application.

2. The return type: The data type of the value returned by the method or void if does not return a value.(TBD)

3. Method Name: the rules for field names apply to method names as well, but the convention is a little different.

4. Parameter list: Comma-separated list of the input parameters is defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, you must use empty parentheses ().(TBD)

5. Exception list: The exceptions you expect by the method can throw, you can specify these exception(s).(TBD)

6. Method body: it is enclosed between braces. The code you need to be executed to perform your intended operations.(TBD)

methods in java

reference:

https://www.geeksforgeeks.org/methods-in-java/

https://www.w3schools.com/java/java_methods.asp

https://www.tutorialspoint.com/java/java_methods.htm

https://www.javatpoint.com/method-in-jav

Leave a comment

Blog at WordPress.com.

Design a site like this with WordPress.com
Get started