by Dinesh Thakur

To create a class variable or method, include the word static in front of the method's name. The modifier static typically comes after any protection modifiers. Given below is an example that illustrates how a class variable may be created in a program.

 
by Dinesh Thakur

Accessor methods are used for initializing and accessing the value of instance variables. The value of these instance variables can be used further in the program. For creating accessor methods, it is required to create two methods among which one method is used to initialize the value and other is used to retrieve the value. An accessor method makes the program more readable and understandable. Moreover, accessor methods are similar to any other method, as can be seen from Program.

 
by Dinesh Thakur

In Java, method definition usually consists of four fundamental parts, the name of the method, the object type or the data type that the method returns (referred to as return type), the list of parameters and the body of the method.



 
by Dinesh Thakur

Since the string variable str is static, it is initialized to "This is second line displayed" (static variables are initialized before an object is initiated). Then, the static block is executed which will invoke the disp() method displaying the message: "This is first line displayed". Static() method can be invoked without the need of any object. In main(), again disp() method is called with the string str (This is second line displayed). After that an object SMV is made, and it will get a member str2 initialized to "This is last line displayed". Then disp() is called with SMV.str2 which displays: This is last line displayed on the screen

 
by Dinesh Thakur

Tail recursion occurring when the recursive call is at the end of the recursive instruction. This eliminates the potential problem of stack overflow. A call is tail recursive if nothing has to be done after the call returns. i.e. when the call returns, the returned value is immediately returned from the calling method.

 
by Dinesh Thakur

In this program, user is asked a number to enter which is stored in integer form in variable n. The value entered is say 1023.Now, this value of variable n is passed to parameter x by invoking revers() method. In revers() method, x is divided by 10 and the remainder is displayed (% returns remainder). When 1023 is divided by 10, the remainder will be 3 which is displayed on the screen. Then, again revers() is invoked with value 102/10 i.e. with 102 (because the output of dividing two integers is also an integer). 102 is assigned to variable x. Again, 102 is divided by 10 and the remainder is displayed. The remainder this time will be 2 which is displayed. Again revers() method is invoked with 10/10 i.e. which 1. And the process continues until value of x becomes 0.

 
by Dinesh Thakur

Recursion occurs when a function calls itself in its own body. That is, in the body of the function definition there is a call to itself. When the function calls itself in its body, it results in an infinite loop. So, there has to be an exit condition in every recursive program.

 
by Dinesh Thakur

There are two ways of passing arguments to a method (function/subroutine):

1. Call by Value

This method copies the value of an argument into the formal parameter of the method. Therefore, changes made to the parameter of the method are limited to that method only and there is no impact of changes on the argument. That is, when control returns back to the caller method, earlier values of the arguments will be seen.

 
by Dinesh Thakur

In any programming, methods or functions are made to support modular programming. By making small functions or methods for each different job not only divides a large project into small manageable tasks but also helps in easy debugging. Methods also make a project scalable that is, it is very easy to expand an existing project which supports modular programming. The methods or a function that we make in java has to be called via objects.