Differences between static and non-static methods in Java

One of the differences that I have found in Java compared to Ruby are the Java Modifiers. Modifier Types are keywords that you add to those definitions you want to change their meanings of. Java has a wide variety of modifiers including Access Modifiers and Non-Access Modifiers.

To use a modifier, you include its keyword in the definition of a class, method, or variable, preceding the rest of the statement, like in the example below:

public int methodName(){ //do something;}

Access Control Modifiers

Access Control Modifiers set access levels for classes, variables, methods and constructors. Access levels can be:

  • Visible to the package, the default. No modifiers are needed.
  • Visible to the class only (modifier private).
  • Visible to the world (modifier public).
  • Visible to the package and all subclasses (modifier protected).

Ruby has something similar with the three levels of protection that it provides. These three levels define the accessibility of a class methods and can be public (methods can be called by everyone outside the class), protected (methods can be invoked only by objects of the defining class and its subclasses) or private (methods can be called only in the context of the current object.)

Non-Access Modifiers

Many other functionalities can be achieved through Java?s Non-Access Modifiers:

  • The static modifier for creating class methods and variables.
  • The final modifier for finalizing the implementations of classes, methods, and variables.
  • The abstract modifier for creating abstract classes and methods.
  • The synchronized and volatile modifiers, which are used for threads.

The Static keyword

The keyword static lets a method run without any instance of the class. A static method belongs to the class, so there?s no need to create an instance of the class to access it. To create a static method in Java, you prefix the static keyword before the name of the method:

public static int min(int a, int b) { //returns the lesser of a and b{

It is possible to combine static and non-static methods in a class, although even a single non-static method means there must be some way to make an instance of the class. Every class you put a main() method in is a class with a static method in it as the main() method is always static.

Characteristics of Static methods

  • A static method is called using the class (className.methodName) as opposed to to an instance reference (new instanceOfClass = class; instanceOfClass.methodName.)
  • Static methods can?t use non-static instance variables: a static method can?t refer to any instance variables of the class. The static method doesn?t know which instance?s variable value to use.
  • Static methods can?t call non-static methods: non-static methods usually use instance variable state to affect their behaviour. Static methods can?t see instance variable state, so if you try to call a non-static method from a static method the compiler will complain regardless if the non-static method uses an instance variable or not.

Non-Static methods

  • A non-static method does not have the keyword static before the name of the method.
  • A non-static method belongs to an object of the class and you have to create an instance of the class to access it.
  • Non-static methods can access any static method and any static variable without creating an instance of the class.

No Responses

Write a response