Classes and Objects
Tags: oop, java, tutorial, beginners
Published: 2019-12-19 17:46:41

Classes and objects can be used to create code that acts similar, or has similar features, but with variations. You could have a class called 'Dog' which stores the names of the dog, the number of legs, and the species. Every dog has a different name, but it is still a dog. A dog may have 4 legs, or it may have 3, but it is still an instance of a Dog.

The below code shows the definition of a Dog class with three fields, or variables, one method, and a constructor. The code snippets are written in the programming language Java, which means the syntax may differ across languages but the concepts will be the same. We will look through each line of this code and explain exactly what is happening here.

```java
class Dog{
	
	public static String species = "canine";
	public int numberLegs;
	private String name;
	
	public Dog(String name){
		this.name = name;
		numberLegs = 4;
	}
	
	public String getName(){
		return name;
	}
	
}

Dog fido = new Dog("Fido");
System.out.println(fido.getName());
System.out.println(fido.numberLegs);
System.out.println(fido.species);
```

The first line in the code snippet defines the class with the name 'Dog', everything within the outer curly brackets is the body of the class, and is what makes up our template of a Dog.

```java
Class Dog{
	...
}
```

Lines 3 to 5 define the fields or variables within our Dog class. The public modifier means that we can access these fields outside of the class when we create instances of the Dog class, and the private modifier means we cannot access the field outside of the class. The static modifier makes a variable shared across all instances. For example, all dogs will have the same species and if one dog object changes that static field it will change for all other instances, whereas the number and legs and name of each instance will be different per instance.

The 'species' is a string variable and is static, and shared across all instances of the class, and is also public which means we can access it outside the class. The 'numberLegs' field is an integer variable type and is public so we can access it from outside the class, but it is not static which means all instances can have different values inside their own version of this variable. The last variable is called 'name', it is a String, it is private which means we cannot directly access is outside the class and it is not static.

```java
public static String species = "canine";
public int numberLegs;
private String name;
```

The next section of our code is the constructor function. This function, or method, is called whenever we create a new instance of the Dog class. Think of it as the mandatory setup of each Dog, what is required and must be initialised when a Dog is created. Within our constructor function we do not declare a return value, as it does not and cannot return any variable. It does take one parameter, a String variable must be passed into the constructor. 

The constructor takes the parameter variable 'name' and assigns its own internal 'name' variable to its value. This means the variable we give to the constructor, the name of the newly created Dog, will be assigned to an internal variable within the class (the variable declared on line 5). We also initialise the internal field 'numberLegs' with a value of 4.

```java
public Dog(String name){
	this.name = name;
	numberLegs = 4;
}
```

The last part of our class defines a public method, a method that we can access from outside the class, called 'getName' that returns a String variable type. This method returns the class's internal variable called 'name', this is an example of a getter method. Calling this function will return the name of the dog that we set within the constructor.

This method is public and therefore can be called outside the class. This method allows us to access the private field 'name' that is defined within the class on line 5.

```
public String getName(){
	return name;
}
``` 

The final bit of our code snippets are written outside of our class and is used to interact with the class. The first line defines a variable called 'fido' that is a Dog variable type, and initialises the variable to a new Dog and passes the string "Fido" to the class constructor. The new keyword is used to create a new instance of the Dog class, and effectively calls the constructor, which is why we must pass a String variable into the creation of the variable.

The next three lines interact with that newly created instance object. We call the 'getName' method of the instance, we can tell it is a call to a method due to the brackets '()' used. This method doesn't take any parameters, so the brackets are empty. This line will print the word "Fido" as that is the return value of that method. 

The next two lines directly access fields from our instances. The first is the 'numberLegs' field, which is a non-static variable. the second is the 'species' field that is static. That means the 'numberLegs' field could be different across all instances of the Dog class, but the 'species' field is the same through all instances.

```java
Dog fido = new Dog("Fido");
System.out.println(fido.getName());
System.out.println(fido.numberLegs);
System.out.println(fido.species);
```

The below code snippet shows how static and non-static fields work. First, we create a new instance of the Dog class called 'buddy' and pass a different name to the constructor. We then change the 'numberLegs' and 'species' fields of our new buddy variable. The print statements then print out those two fields, 'numberLegs' and 'species' from both our 'fido' instance and our 'buddy' instance. The number of Legs will be the number 4 for 'fido' but the number 3 for 'buddy' because it is non-static. The field 'species' will print "Mammal" for both instances, because it is a static variable and therefore shared across all instances.

```java
Dog buddy = new Dog("Buddy");
buddy.numberLegs = 3;
buddy.species = "Mammal";

System.out.println(fido.numberLegs);
System.out.println(fido.species);

System.out.println(buddy.numberLegs);
System.out.println(buddy.species);
```

This article was originally posted on my website: https://acroynon.com/