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.

Sunday, March 9, 2014

Convert a negative number to a positive number


Code sample on pastebin

"Math.abs (Java Platform SE 7 )" ( http://bit.ly/1hWJjy9 )

Consider the following example where we want to calculate the distance between 2 axis coordinates.

int x1 = 200;
int x2 = 300;
int distance = x1 - x2;

The result is distance =  -100.

If we are simply interested in the distance and not whether the distance is a negative, we can use Math.abs to get the absolute (positive) value of distance, like so:

int distance = Math.abs(x1 - x2);

The result is distance =  100.

Literal



A literal is any number, text, or other information that directly represents a value.


Literal format of String

eg.
String s1 = "This is the literal format of String";

"This is the literal format of String" is the String literal.

This is as opposed to:

String s2 = s1;

where s2's value is a reference to another variable.


Literal format of int

eg.
int num1 = 500;

500 is the int literal.

This is as opposed to:

int num2 = num1;

where int2's value is a reference to another variable.


For a listing of literals for all Java types also see:
"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )

Friday, March 7, 2014

Using the Random class to generate a random int

Code sample at pastebin


The Random class

java.util.Random
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Importing java.util.Random

You will need the following imports statement at the top of your code file:
import java.util.Random;

Generate any random number

Random r = new Random();
r.nextInt();
generates a random number in the int range (-2,147,483,648 to 2,147,484,647).

Generate a random within a maximum range

Random r = new Random();
r.nextInt(max);

generates a random number in the positive int range, inclusive of 0 but exclusive of max.
eg.
r.nextInt(500);
generates a number from 0 to 499. If you want to include 500 in the range, add + 1.

r.nextInt(500 + 1);

Generate a random within a minimum and maximum range

Random r = new Random();
r.nextInt(max - min) + min;

generates a random number in the positive int range, inclusive of min but exclusive of (max - min) and then adds the minimum to correct the range.

eg.
r.nextInt(500 - 300) + 300;

Generates a number from 0 to 299. You can then add your minimum so now the number has the range of 299 to 599. Remember, the actual number will be exclusive of the max so if you want to include the max add + 1.

r.nextInt((500 - 300) + 1) + 300;



Here's a cat.

Tuesday, November 26, 2013

Abstract classes and methods

"Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/1gcH6gn )

Wednesday, November 6, 2013

Reflection: Instantiation


Class<?> cl = Class.forName("javax.swing.JLabel");
Constructor<?> cons = cl.getConstructor(String.class);
Object o = cons.newInstance("JLabel");