What

My first Java applet was a monster of inefficiency, a far cry from the elegant brevity that is the mark of an experienced programmer. This was largely due to my failure to exploit the power of well-placed subroutines.

A common question among Java newbies is “How do I know when to use a method?” The short answer is: “Any time a specific block of code will be called more than once within your program.” The best way to make sense of how methods can help you refine your code is to examine a method definition. (Please see the corresponding chart.)

Access Modifier: Used for both method definitions and variable declarations, the access modifier specifies whether that method or variable is visible from outside the class. The Public modifier gives access to all classes. The Private modifier stipulates that the method or variable is only accessible within the class that defines it. This is integral to the concept of data hiding. It is generally accepted as good practice to declare all variables as Private and use set and get methods to modify and view their contents.

The third modifier is Protected. This grants access to classes that extend the defining class, but hides the data from all other classes (the next column in this series will discuss the class hierarchy in depth).

Static Declaration: The static keyword is used to define a class method or class variable (as opposed to an instance method or instance variable). Consider this example of how static variables are useful:

Suppose you have written class Car to describe an automobile. Because cars can be different colours, carColour may be defined as an instance variable. Every object of type Car that was instantiated in your program would have its own copy of the carColour variable. If you wanted to create a counter that would keep track of how many cars there were, it would make sense to define carCounter as static (a class variable). This means that there is only one carCounter variable stored in memory, and its value is the same for every Car object.

This considered, it is easy to see why the main method of a Java program must be defined as static. If a program were allowed to create new instances of its own main method, it would re-invoke itself ad infinitum and the system would hang. Keep in mind that a static method cannot make reference to non-static variables.

Return Type: Some methods return a value after executing. The return type specifies what the format of the return value is. If you wished to create a method that would concatenate firstName and lastName to return fullName, the return type would be String. Similarly, a method used to return the result of a percentage calculation may return the double data type. Methods that do not return anything have a return type of void.

Method Name: You can define more than one method with the same name but different parameters (see below). This is known as method overloading. When called, the method will behave differently depending on the data passed to it.

In order to define a constructor, create a method with the same name as the class in which it appears. A constructor is a method that gets called automatically when the class is instantiated (an object of the type it describes is created in the program). A null-argument constructor is one that is defined without parameters. The compiler will write this constructor for you if you fail to do so yourself. Constructors have no return type (not even void).

Parameters: In the method definition, the variables declared to hold incoming data are called parameters. In the method call, the variables being passed are called arguments. The official reason behind giving two names to what is essentially the same data is that it makes our job sound more complicated, so those who don’t know Java will keep paying us The Big Money.

The arguments in a method call must be compatible with the parameters defined. For example, a call to the method shown in the diagram may look like this:

doCalcs(15, 7);

Two integers are passed to the method. Their values (15 and 7) are now stored in method variables named valueOne and valueTwo, and the doCalcs method can perform operations with them. The topic of methods is very complex, and this will surely be the most involved column in the series. If it proves to be difficult reading, don’t get discouraged. Just keep The Big Money in the back of your mind as you forge ahead.

Part three of this series, to appear in issue 12, will explore the class hierarchy.

Cooney is a Toronto-based programmer and a freelance Internet developer. He is currently enrolled in Humber College’s Computer Programmer program. He can be reached at robert_cooney@hotmail.com.

Would you recommend this article?

Share

Thanks for taking the time to let us know what you think of this article!
We'd love to hear your opinion about this or any other story you read in our publication.


Jim Love, Chief Content Officer, IT World Canada

Featured Download

Featured Articles

Cybersecurity in 2024: Priorities and challenges for Canadian organizations 

By Derek Manky As predictions for 2024 point to the continued expansion...

Survey shows generative AI is a top priority for Canadian corporate leaders.

Leaders are devoting significant budget to generative AI for 2024 Canadian corporate...

Related Tech News

Tech Jobs

Our experienced team of journalists and bloggers bring you engaging in-depth interviews, videos and content targeted to IT professionals and line-of-business executives.

Tech Companies Hiring Right Now