Programming For Non-Techies - Java Classes

Programming For Non-Techies - Java Classes

Programming For Non-Techies is a weekly feature for non-techies to learn to program every Friday.  If it’s your first time here please review the previous lessons in the archives to get caught up.  This week’s lesson continues the discussion about variables and we will look at Java classes (class types).   If you’re a non-techie and you want to learn to program, or maybe you want a job in tech and need a better understanding of programming, these posts will help you!

If you’re new here, I strongly suggest looking at the previous programming posts since they build on each other.

Java Classes

You can’t talk about classes before, first, talking about objects.  Objects are used to represent the state or behavior of something.  Classes are the template or blueprint used to describe and define an object.

Let’s look at our previous example.  We wrote a class called ExampleProgram.  When we want to create an actual object from that class we use the ‘new’ keyword to instantiate or create an instance of that class.  We did that on line 4 and instantiate an object named ‘example’.

ExampleProgram example = new ExampleProgram();

Inside the class, we define several variables, which are used to represent the state of the object.  We also define many methods.  We use methods to represent behavior.  Here’s one line from last week’s example:

private void ifThen(int x)

Let’s look at Java code

We’re moving on to new example code this week.  Create files in eclipse named FootballField.java and FootballPlayer.java and paste the appropriate code in each.

First, let’s look at FootballField.  After declaring the class, the next thing we do is define a variable named player that’s of the type FootballPlayer.  Note that the ‘private’ keyword, as well as the ‘public’ keyword, set the scope of the variable or class.  Scope is the term we use to describe what can access variables or methods.  We’ll cover it in more detail soon.

The next two blocks of code are called accessor methods but are commonly referred to as getters and setters.  In this case, they allow you to get or set the player variable.  The getPlayer() method returns a FootballPlayer object.  setPlayer() doesn’t return anything and that’s designated with the ‘void’ keyword.

The setPlayer() method takes an argument of a FootballPlayer and we named it ‘player’.  We use the ‘this’ keyword to define which ‘player’ we are referring to.  In this case, we pass in player and then set it in the object’s player.

toString() is a standard method to output an object into a String.  Without it, it would print something a lot less usable.  We will tackle Strings another day, but try removing the toString() method and see what happens.  We also created a main() method so that we have something to execute.  That should be familiar from the previous example.  For this one, we instantiate both objects and use the methods we created to set values.

Now, look at the FootballPlayer class.  The code is similar to the FootballField class minus the main() method.  Run that main method.  What did you get?  Let us know in the comments if you run into any problems.  See you next week!

Example Code

/**
 * Football Field example
 */

/**
 * @author techfornontechies.com
 *
 */
public class FootballField {
	private FootballPlayer player;

	public FootballPlayer getPlayer() {
		return player;
	}

	public void setPlayer(FootballPlayer player) {
		this.player = player;
	}
	
	@Override
	public String toString() {
		return "FootballField [player=" + player + "]";
	}

	public static void main(String[] args) {
		FootballField field = new FootballField();
		
		FootballPlayer player = new FootballPlayer();
		player.setName("Techie");
		player.setNumber("98");
		
		field.setPlayer(player);
		
		System.out.println(field);
	}
}
/**
 * Football Player example
 */

/**
 * @author techfornontechies.com
 *
 */
public class FootballPlayer {
	private String number;
	private String name;
	
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "FootballPlayer [number=" + number + ", name=" + name + "]";
	}
}

Leave a Comment