How to Write and Reuse a Java Class

Creating a Simple Class in Java and Reusing It

© Mark Alexander Bain

Jan 21, 2009
A Java Class, Mark Alexander Bain
Java is a oriented programming language and therefore consists of classes. This simple tutorial shows how to create a Java classes and how to reuse the code.

The one key thing to remember about programming in Java is that it's all about classes. Java is an object oriented programming language and even the simplest Java program is an example of a class:

public class helloworld {
public static String greeting_text = "Hello World";
public static String greeting () {
return greeting_text;
}
public static void main(String args[]) {
System.out.println(greeting());
}
}

This code must be saved in a text file named helloworld.java (the name of the text file must always be the same as the class), and then the class can then be compiled:

javac helloworld.java

and run:

java helloworld

Here Java will create an instance of the class (otherwise known as an object) and then run the main method. The end result will be the words "Hello World" displayed on the screen. However, the real power of object oriented programming is, of course, that this simple class can easily be incorporated into another one.

Reusing Java Classes

The code for a class can now be reused in other class by making use of the new method:

public class helloworld_use {
public static void main(String args[]) {
helloworld myhello = new helloworld();
myhello.greeting_text = "Hello again";
System.out.println(myhello.greeting());
}
}

And again the code can be compiled and run:

javac helloworld_use.java
java helloworld_use

The end result is the words "Hello again" written to the screen, but the key thing here is that the new application requires no new code to be written for that to happen.

Inheritance and Java

In the above example the helloWorld class is loaded as a separate object into the new class, and then it's methods and properties can be accessed. However, there is another way of using a class - by extending it:

public class helloworld_ext extends helloworld {
public static void main(String args[]) {
greeting_text = "And hello once more";
System.out.println(greeting());
}
}

This time all of the methods and properties of the original class are inherited by the new one and can, therefore, be used as part of the new class without having to refer to the original.

Summary

Java is an object oriented programming language and all Java applications consist of one or more classes. These classes can be reused again and again in one of two ways:

  • by using the new method to create an instance of the class
  • by using the extends key word to inherit the methods and properties of a class

With just those simple techniques even the most complex applications can be build quickly and easily, with each application consisting of a class which itself is a combination of one or more other classes


The copyright of the article How to Write and Reuse a Java Class in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Write and Reuse a Java Class in print or online must be granted by the author in writing.


A Java Class, Mark Alexander Bain
Create a Java Class, Mark Alexander Bain
Reuse a Java Class, Mark Alexander Bain
Extend a Java Class, Mark Alexander Bain
Compile and Run Java Code, Mark Alexander Bain


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo