1
class MergeSort {

    // Merge two subarrays L and M into arr
    void merge(int arr[], int p, int q, int r) {
    }
  
    // Divide the array into two subarrays, sort them and merge them
    void mergeSort(int arr[], int l, int r) {
    }
  
    // Print the array
    static void printArray(int arr[]) {
    }
  
    // Driver program
    public static void main(String args[]) {
      int arr[] = { 6, 5, 12, 10, 9, 1 };
  
      MergeSort ob = new MergeSort();
      ob.mergeSort(arr, 0, arr.length - 1);
  
      System.out.println("Sorted array:");
      printArray(arr);
    }
}

I am completely new to Java, I started it from today itself, but I do know basic of OOP's concepts. My question is Why are we creating an instance(ob) of MergeSort in void main? main method is already present inside the class MergeSort, it can directly access the function mergeSort right ? and why are we not using instantiated object of MergeSort to call printArray(arr), we are able to call printArray directly so why not mergeSort method.

Also please suggest me some sources to learn java better if you have one.

Thank you for your help.

marsaldev
  • 3,338
  • 2
  • 16
  • 27
Hackytech
  • 317
  • 2
  • 11

2 Answers2

3

it can directly access the function mergeSort right ?

No, it can't, because mergeSort isn't declared as a static function, which means you need an instance of MergeSort in order to call it. That's why you're able to call printArray, because it is static, which means you don't need an instance of the enclosing class to call it.

More info on static members and methods: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

nickb
  • 59,313
  • 13
  • 108
  • 143
1

First of all, Welcome to Java World ;-)

My question is Why are we creating an instance(ob) of MergeSort in void main?

Well, all applications have got a starting point and usually all c-based programming languages start from Main function (method). Java's starting point is the static main method which is located in a class and as a convention that class is called Main.

Java is an object oriented programming languages, so Java applications should consist of many classes and these classes can communicate and interact with each other based on object oriented rules. To use a class, you need to create an instance of it and you have no choice to create your first instance from the main method.

main method ..., it can directly access the function mergeSort right ?

Main method is a static method and you can directly call all static methods from it, but mergeSort method is defined as an instance method (not static) and there is no way to call it directly. You need to create an instance of class and then call its methods.

MergeSort ob = new MergeSort();
ob.mergeSort(arr, 0, arr.length - 1);

why are we not using instantiated object of MergeSort to call printArray(arr)

Because printArray is a static method and you can easily call it from the main method or any other static methods. Read all answers of the relevant question, it can help you.

Please notice that your code-snippet can be classified as a single-source-code application which means that you have only one. Single file applications are small and utility programs. But real applications are made of various classes and files.

please suggest me some sources to learn java better if you have one.

It's not common to suggest learning sources in Stackoverflow, but I would highly recommend to follow an online video tutorial and then books. Pluralsight and Linkedin have got very good learning paths.

Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54