What are static Methods, static Fields and Class Math in Java? || Why main() Method is declared static? || Easy Learn Java

Although most methods execute in response to method calls on specific objects, this is not always the case. Sometimes a method performs a task that does not depend on the contents of any object. Such a method applies to the class in which it’s declared as a whole and is known as a static method or a class method. It’s common for classes to contain convenient static methods to perform common tasks. 

To declare a method as static, place the keyword static before the return type in the method’s declaration. For any class imported into your program, you can call the class’s static methods by specifying the name of the class in which the method is declared, followed by a dot (.) and the method name, as in

ClassName.methodName( arguments )

 
We use various Math class methods here to present the concept of static methods. Class Math provides a collection of methods that enable you to perform common mathematical calculations. For example, you can calculate the square root of 900.0 with the static method call

Math.sqrt( 900.0 )

The preceding expression evaluates to 30.0. Method sqrt takes an argument of type double and returns a result of type double. To output the value of the preceding method call in the command window, you might write the statement

System.out.println( Math.sqrt( 900.0 ) );

In this statement, the value that sqrt returns becomes the argument to method println. There was no need to create a Math object before calling method sqrt. Also all Math class methods are static—therefore, each is called by preceding its name with the class name Math and the dot (.) separator.

Method arguments may be constants, variables or expressions. If c = 13.0, d = 3.0 and f = 4.0, then the statement
System.out.println( Math.sqrt( c + d * f ) );

calculates and prints the square root of 13.0 + 3.0 * 4.0 = 25.0—namely, 5.0.

Method

Description

Example


abs( x )absolute value of xabs( 23.7 ) is 23.7
abs( 0.0 ) is 0.0
abs( -23.7 ) is 23.7
ceil( x )rounds x to the smallest integer not
less than x
ceil( 9.2 ) is 10.0
ceil( -9.8 ) is -9.0
cos( x )trigonometric cosine of x (x in radians)cos( 0.0 ) is 1.0
exp( x )exponential method exexp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
floor( x )rounds x to the largest integer not
greater than x
floor( 9.2 ) is 9.0
floor( -9.8 ) is -10.0
log( x )natural logarithm of x (base e)log( Math.E ) is 1.0
log( Math.E * Math.E ) is 2.0
max( x, y )larger value of x and ymax( 2.3, 12.7 ) is 12.7
max( -2.3, -12.7 ) is -2.3
min( x, y )smaller value of x and ymin( 2.3, 12.7 ) is 2.3
min( -2.3, -12.7 ) is -12.7
pow( x, y )x raised to the power y (i.e., xy)pow( 2.0, 7.0 ) is 128.0
pow( 9.0, 0.5 ) is 3.0
sin( x )trigonometric sine of x (x in radians)sin( 0.0 ) is 0.0
sqrt( x )square root of xsqrt( 900.0 ) is 30.0
tan( x )trigonometric tangent of x (x in radians)tan( 0.0 ) is 0.0

Math Class Constants PI and E

Class Math declares two fields that represent commonly used mathematical constants— Math.PI and Math.E. Math.PI (3.141592653589793) is the ratio of a circle’s circumference to its diameter. Math.E (2.718281828459045) is the base value for natural logarithms (calculated with static Math method log). These fields are declared in class Math with the modifiers public, final and static. Making thempublic allows you to use these fields in your own classes. Any field declared with keyword final is constant—its value cannot change after the field is initialized. PI and E are declared final because their values never change. Making these fields static allows them to be accessed via the class name Math and a dot (.) separator, just like class Math’s methods. Each object of a class maintains its own copy of an attribute, the field that represents the attribute is also known as an instance variable—each object (instance) of the class has a separate instance of the variable in memory. There are fields for which each object of a class does not have a separate instance of the field. That’s the case with static fields, which are also known as class variables. When objects of a class containing static fields are created, all the objects of that class share one copy of the class’s static fields. Together the class variables (i.e., static variables) and instance variables represent the fields of a class.

Why Is Method main Declared static?

When you execute the Java Virtual Machine (JVM) with the java command, the JVM attempts to invoke the main method of the class you specify—when no objects of the class have been created. Declaring main as static allows the JVM to invoke main without creating an instance of the class. When you execute your application, you specify its class name as an argument to the command java, as in

java ClassName argument1 argument2 …

The JVM loads the class specified by ClassName and uses that class name to invoke method main.
In the preceding command, ClassName is a command-line argument to the JVM that tells it which class to execute. Following the ClassName, you can also specify a list of Strings (separated by spaces) as command-line arguments that the JVM will pass to your application. Such arguments might be used to specify options (e.g., a file name) to run the application. Arrays and ArrayLists, your application can access those command-line arguments and use them to customize the application.

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...
 
Posts RSSComments RSSBack to top
© 2013 Updated Tech News Results and Reviews