top of page
  • Writer's pictureMr R

There is a Right and a Wrong Way to Java

As discussed in the other posts in this series on Object-Oriented Programming, there are a few coding conventions that we have made references to such as getters/setters, private and public variables and the use of the this keyword.


Private and Public Members


As mentioned in Getting Classy with Classes we can categorise the methods and variables of a class as either public or private. By default all members of a class are set to public, this means that they can be directly accessed internally within the class and externally. Private members cannot be accessed externally by other objects or classes, these members can only be called internally by other members of the class.


We can use private members as a form of inbuilt security to avoid the wrong classes or other agents from accessing and changing data we do not want changed. Though because of their nature we need to use methods that we call getters and setters, to be able to incorporate these members into our system.


Getters and Setters


Getters and setters are the names we give to certain methods that we use to access and change our private variables, because of this we almost always make these public methods. As the names imply getter methods are used to get and return the value of a private variable. Meanwhile setter methods are used to set or change the value of a private variable. Below are some examples.


Basic getter method example:

private int counter = 0;

public int getCounter()
{
    return this.counter;
}

Basic setter method example:

private int counter = 0;

public void setCounter(int value)
{
    this.counter = value;
}

The above examples display the basic uses of getters and setters, though this is not the only way these can be built and we can include more complex functionality inside them.


Description getter method example:

private String name = 'David Jones';
private int age = '19';
private String gender = 'male';

public String getDescription()
{
    return name + ' is ' + (String) age + ' and identifies as ' + gender;
}

Incrementor setter method example:

private int counter = 0;

public void incrementCounter()
{
    this.counter += 1;
}

public void decrementCounter()
{
    this.counter -= 1;
}

These can be combined with other functions as well. A common type of getter is the toString() method, used to output the full contents of the object in string format.


This and Super


As demonstrated in pervious examples in other posts we sometimes use the this keyword when referencing variables in a class. You may have wondered why we sometimes add this on to the declarations. We use the this keyword to reference anything internal to the class. This is done to help distinguish the class members from temporary parameters as part of a method.

public class ExampleClass
{
        
    private String message;
        
    public ExampleClass(String message)
    {
        // We differentiate using the 'this' keyword
        this.message = message;
    }
    
}

The super method is used for referencing the parent to the current class. This means that when building a class which extends a parent we can reduce redundancy in the child class by calling the parent constructor in the child class. We are also able to call parent member methods instead of having to recreate the functionality of overwritten methods.

public class ParentClass
{
    
    private String x;
    
    public ParentClass(String x)
    {
        this.x = x
    }
    
    public displayMessage()
    {
        System.out.println(x);
    }
    
}

public class ChildClass extends ParentClass
{
    
    private String y;
    
    public ChildClass(String x, String y)
    {
        this.y = y;
        Super(x);
    }
    
    public displayMessage(String message)
    {
        System.out.println(y);
        super.displayMessage();
    }
    
}

Food for thought


How might you use these methods to rewrite your previous code to reduce redundancy and make your code safer. Are there any other things you think may be useful to do to write better code?

Recent Posts

See All

Classes of Classy Classes

A breakdown of the main categories of object-oriented classes, their usages and best practices.

bottom of page