Basic Java – 3 || Understanding Class, Objects, Methods in Java.

               A Class is like a repository which contains all data members and methods. On the other hand, an Object is an instance of a class which acquires all the properties(data members) and behavior(methods) of the class.

Main_Method
                              Main Method Structure
Related Links:

OOPs Concept:

Java Question And Answer:

Method

          In general, if we talk method is the way to achieve a functionality. As we have to perform multiple steps to achieve the goal, the same way a method is a set of Java statements in a logical sequence to achieve a functionality.

Java Program Structure

  1. A Java program must have at least one class.
  2. A Java program must have one main() method.
  3. A Java program may contain any number of variables(data members) and methods.

Method Structure

< Access Modifier > <Non-access modifier><Return type><Method Name>(Param_1,Param_2,..)

{

               <Method Body>

              return(return_value);

}

Understand the usage of different keywords while defining any type of method

Access Modifier (public) − It defines the access type of the method and it is optional to use.

Non-Access Modifier (static) – In general, the method will be available independently for all the instances of the class. To call a static method there is no need to create an instance of the class. This is also optional.
ReturnType (void) − Methods may or may not return a value. This should be defined as void or any data type.

Method Name (main()) − This is the method name. The method signature consists of the method name and the parameter list.
Parameter List (String[] args) − The list of parameters, it is the type, order, and a number of parameters of a method. These are optional, a method may contain zero parameters.
Method Body(Java Statements) − The method body contains the java statements in a logical sequence to achieve a functionality.

Return Value – Mention the value which needs to be returned at the end of the function. Data type should be same as ReturnType.

Sample of main() Method

Class TestClass
{
        public static void main(String[] args)
        {
          Method Body…..
          Java statement….1
          Java statement….2
          Java statement….3  

        // No need to return any value as the return type is void.
       }
}

Facts about main() Method

  1. A main() method is an entry point for a java program.
  2. main() would be called by the JVM using the class directly without creating an object. That’s why we declare the main method ‘public’ and static.
  3. main() would never return any value, so we kept return type void.
  4. main() would take parameters in the form of String Array. Which would be used to passing input values at runtime.

Code Example

public class TestClass {

	public static void main(String[] args) 
	{
		System.out.println("Default method of a java program.");
	}
}	

            In the above example, we observe that there is no need for any object to run the program. This is a simple example having only one method. In real life, we have to declare multiple methods in a single class.

            Now take another example which explains the creating and usage of the object. It contains both types of methods Static and Non-static.

public class TestClass {

	public static void main(String[] args) 
	{
		System.out.println("Default method of a java program.");
		
		// Static method can be called without any object.
		getCompanyData();
		
		// Nonstatic method can not be called without any object.
		// Compile time error would appear.
		//myProfile();
		
		// Calling non-static method using Object.
		TestClass obj=new TestClass();   // Object declaration and initialization.
		obj.myProfile();                 // Calling non static method.
	}

	public static void getCompanyData()
	{
		System.out.println("\nStatic Method Called...");
		System.out.println("Company Name");
		System.out.println("Designation");
	}
	public void myProfile()
	{
		System.out.println("\nNon-Static Method Called...");
		System.out.println("User Name");
		System.out.println("User Address");
		System.out.println("Mobile");
	}
}

2731total visits,4visits today

Leave a Reply

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