by Dinesh Thakur Category: Classes

Apart from class and instance variables, Java also has class and instance methods. The differences between the two types of method are analogous to the differences between class and instance variables. Class methods are available to any instance of the class itself and can be made available to other classes. Therefore, some class methods can be used anywhere, regardless of whether an instance of the class exists or not.

In order to define class methods, the keyword static is used. This keyword should be placed in front of the method definition.

For example, the following illustration defines a class method named Max.

static int Max(int arg1. int arg2) { ... }

In Java, each data type has a wrapper class. Wrapper classes for integer, float and boolean data types are Integer, Float and Boolean, respectively. By using class methods defined in these classes, objects can be converted into data types and vice versa. For example, the parselnt() class method in the Integer class takes a string and a radix (base) and returns the value of that string as an integer:

Int count = Integer.parselnt("42", 10) // returns 42

Usually methods that operate on a particular object and affect it in one way or the other are defined as instance methods. Methods that provide some general utility but do not directly affect an instance of that class are declared as class methods.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles