All About Methods in Java

0
81
Spread the love
  • 24
  • 34
  • 16
  • 12
  •  
  •  
  •  
  • 12
  •  
  •  
    98
    Shares

$javac MyTest.java

$java -Xmx128M -Xms16M MyTest Exception in thread “main” java.lang.IllegalArgumentException: value must be greater than zero at MyTest.myNumberTest(MyTest.java:5) at MyTest.main(MyTest.java:10) so if this method gets a negative or zero value, it will immediately terminate the execution by throwing an exception.

Read Method overloading here for more about this technique.

What Is Public Static Void Main Method in Java?

Main method in Java

For standalone java program,the class name should be same as file name. we will get a compilation error if otherwise.The class should contain a main() method with the given signature:


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

Conceptually every java program must include a main() method to run independently.main() is the starting point or triggering point for interpreter to begin execution.A java program can have n-numbers of classes but only one of them include  a main() method to initiate the execution.

public-> is available for the world to use.
String[] args->It declares a parameter name args of type String array.The argument to the main is an array of String objects.In IDE mode of development,they will never be used in general,but they hold the arguments invoked on the command line.
Static:Static keyword declares this method /variable as one that belongs to the entire class and not part of any objects of the class. The main() method must always be declared as static since the interpreter uses this method before any objects are created.Non static things belong to object.If main is non static,we can not have main method until we create an object.But when we start java code,no objects have been created yet. The statements that are executed in the main method start creating objects.
void–>this modifier declares that main() method does not return any value.

Methods with Varargs in Java

All About Methods with Varargs in Java:

Varargs represents variable length arguments in method. This makes java simple yet very powerful.

The general syntax is given as:


<access specifier><return type>methodName(<argument type>... arguments)
 

This method contains an argument referred as varargs in which we are going to pass the specific type of arguments.
An ellipsis(…) is the key to varargs and argument is the name of the variable.


public void registration(String userName,String password,String emailId);
//can be written as:
public void registration(String... registrationDetails)
 

String… registrationDetails specifies that we can pass any number of String arguments to the registration() method.
How to retrieve the values:


class Registration{
testVarargs(String... registrationDetails)
{
for(String details:registrationDetails)
{
System.out.println(details);
}
} 
public static void main(String args[])
{
testVarargs("MydigitalVoice","abc123","[email protected]");
}
}
 

At compile time the String… registrationDetails is converted to String[] varargs. we can pass any array of Strings to the methods.Varargs does not generate any compile time errors even if an empty argument is passed as a parameter to the method.

What is Finalize method in Java?

Finalizer Method:

when constructor initializes an object,the mechanism is called initialization.Similarly java supports one more method just opposite to this,called finalization. Java garbage collector frees up memory used by the objects.But objects may hold other non objects (not created by Java new operator) resources(like file descriptor,window system fonts etc).Garbage collector can not free up these items as they as not created by new operator. To reclaim such memories,finalize() method is used. Finalize () method needs to define explicitly.

Cleanup:

Many a times programmers forget to cleanup memory.Nobody wants to clean up the small chunk of memory for int or character like this approach. To free up the memory java came up with a concept called Garbage collector to reclaim the memory of objects those which are no longer in use. But garbage collector can only reclaim memory for objects which are created by ‘new’ operator. If some program is occupying memory using some other way,Garbage collector won’t be able to reclaim those memories. To handle such condition, Java has provided a method called finalize().This method can be defined for any class.

How it works?

When Garbage collector is ready to release the storage of the object,it calls finalize() method first.And only the next Garbage collection pass will reclaim the memory for the object.We can override finalize() method by redefine the method.

We have to understand the below points while working with Garbage Collector:

  1. Garbage collection is not object destruction. As java has no concept for destructor for clean up memory. It just ensures if there is some activity that must be performed before it reclaim the memory. We must perform that activity by ourselves.We can create our own methods to cleanup the memory.
  2. All objects may not get garbage collected.It may so happen that our program never face the memory shortage issue(may be the code is too small and the available memory is too high).In that case garbage collector may reclaim the memory.So after termination of the program the memory is returned to the Operating system.Garbage collection process has some overhead,in the above way we never have to incur the expense.
  3. Garbage collection is not only about memory reclaim. If we use as synonym of memory release the finalize has to be same.

The role of Finalize():

Now if we create some other objects in our code and call finalize() method,will they be released? No it won’t. Garbage collector will take care of the release of other objects in the memory,irrespective of how the objects are created. Since everything in java are object,in that case where we will define finalize() method?
Well,Java supports native methods(C,C++ etc).In these native languages there are ways to allocate memories like calloc(),malloc().Unless we call free() method,the storage will never be released,causing memory leak in java code via native methods.We can define the C language free() method in the other function where all our functions were defined.It is a best practice to write free() method inside finalize() method.Java does not allow us to create local objects rather all objects should be created using new operator.
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 16
  • 12
  •  
  •  
  •  
  • 12
  •  
  •  
    98
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here