Showing posts with label Static. Show all posts
Showing posts with label Static. Show all posts

Monday, March 24, 2014

Why the main method is static

In Java, there must be a main method in at least one class. This is the front door to the application. The runtime system begins with a call to main and in turn, the main method calls other functions of the program.

The main method is a special case and is always declared with the static keyword - this means that the method belongs to the class rather than to an instance of the class - there is only one.

All internal members (attributes) and methods which are called by main, must have the static modifier.

If you attempt to call a non-static internal method from a static method, you will get a compile error, as in below:


Notice that the call to the doSomething() method does not have errors. This is because it has been defined with the static keyword. The call to doSomethingElse(), however, has errors because it is a non-static method.

Similarly, attempting to call a non-static member from the static method will produce compile errors:



Notice that the call to the username member does not have errors. This is because it has been defined with the static keyword. The call to password, however, has errors because it is a non-static member.

This is different to an instance (a regular object) where there may be many instances. In this case, members and methods belong to each instance and are not static.

Monday, September 24, 2012

Generating a random number

also see Using the Random class to generate a random int

The Math class contains a static method for random number generation.

To generate a single random int between 0.0 and 1.0 you can use the following:

int number = (int)(Math.random() * 10);


This method returns a value from 0.0 to 0.9. A whole number, from 0 to 9, can be produced by multiplying the result by 10.

Note that casting has been used to convert the return value, which is a double, to int.

And remember, static class methods may be called without creating an instance of the class.

Where many random numbers with a larger range are required, see java.util.Random such as below:

In this example we are going to generate random numbers between 1 and 100:


The statement rand.nextInt(100) + 1 gets the next random number in the range of 1 - 100 inclusive. Need a large number? Adjust to suit.


Friday, May 18, 2012

The 'static' keyword


Download the code


Static variables & methods are associated with the class rather than an instance of an object of that class.

Usually, the data contained within an object, belongs only to that instance.

If you want all instances of a class to share some data, use a static variable. A static variable belongs to the class, rather than an instance the class (object).

The example below uses a static variable to keep count of the number of Widget objects that have been created.

The Widget class contains a single static variable called _numberOfWidgets. No matter how many instances of Widget are created, there will be a single value associated with the _numberOfWidgets variable.

A static method is provided to set the value of _numberOfWidgets and a static method is provided to get the value of _numberOfWidgets. In this example, the constructor calls the set method and in turn, the set method increments the value.


In the TestWidgetClass below,  a number of Widget instances are created. Note that the call to the get_numberOfWidgets method is made by referencing the class directly, rather than by instance of the class.



And the output is:




"Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/KmbYd7 )

In a UML class diagram, Static variables & methods are represented with an underline.



Here's a cat to keep you calm:


Monday, March 19, 2012

The Static keyword

The static keyword is a Java modifier which associates a class member or method with a class rather than an instance of a class.

For instance, the main method of a class is always static as there must be exactly one instance of the class containing main in order for the compiler to find it.

A class containing static members or methods does not require instantiation in order to use the static methods of the class.

Sunday, March 18, 2012

The Main method

A Java project many contain many packages and each package may contain many classes. There must be at least one class containing a main method.

The main method is a static method, belonging to the class, where program execution begins - it's the front door, so to speak. The main method begins the chain of events that load the program into memory, ready for use.

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)


The main method can optionally take a String array as a parameter, as shown in the parenthesis.

(String[] args)