programming, technology

Thinking in objects and classes – Java — final (Part 5)


Miscellaneous

There are some things that we cannot exclude when we talk about OOP. But we’ve done that so far.. oops! We’ll cover them all shortly. They include class variables, class methods and constants.

Class variables

Like the name suggests, they belong to the class. They do not vary for each object like we’ve seen in previous variables (attributes). You can access them with the class name like this Person.variable_name.

Let’s look at it from a different angle. We may have an attribute called population. This variable shouldn’t be different for each “person” because population is the total number of all persons. When a new person is created, that value changes and it changes for every person.

Class methods

We may also apply the same idea to methods. Let’s say scientists decide that humans are no more Homo sapiens and so we have to change that. It’ll be ideal to change that with a class variable. As you might have guessed, we reference them using the class name. (These are conventions but very necessary for clarity.).

Action time

We’ll create two class variables, and two class methods and see how they look like in code.


public static int population = 0;

…and that’s how to create a class variable in Java. The static keyword makes it a class variable. Add that line of code to the Person class beneath the other attributes. Also add the line of code below to the two constructors we created in the Person class in our previous lesson.


this.population ++;

This means that, anytime we create an object, the population is going to increase by 1. Cool huh?! 😉

Let’s add our second class variable. We’ll call it genus_species. We’ll also create two methods for this variable; a setter and a getter. Setters and getters are simply methods that access private variables. Setters set a new value to them while getters return their current value.

Add this line of code to the attributes in Person class.


private static String genus_species = "Homo sapiens";

Then create the two methods. We’ll call them setGenusSpecies() and getGenusSpecies().


public static String getGenusSpecies(){
return genus_species;
}

public static void setGenusSpecies(String value){
genus_species = value;
}

Notice the difference between the two. Getters return the attribute so they have a return type but they take no argument(s) whereas setters take argument(s) and set the attribute to the argument.

…”finally”, let’s talk about constants, or static final variables.

Let’s talk about constants then we can go ahead and have a full view of the code and also have some demonstrations.

Constants are by convention, upper case. They cannot be set after declaration. Once they’re set, it’s final. Let’s take a look at their syntax.


public static final String PLANET = "Earth";

That’s that. Nothing strange. What you should note is, you cannot set a new value even though it’s public. You cannot do this: Person.PLANET = "Mars" for instance. You can only get constants.

Demonstrations


//Person.java
package javaapplication1;

public class Person {
protected String first_name;
protected String surname;
protected String location;
protected int age;

public static int population = 0;
private static String genus_species = "Homo sapiens";
public static final String PLANET = "Earth";

public Person(String first_name, String surname, String location, int age){
this.first_name = first_name;
this.surname = surname;
this.location = location;
this.age = age;
this.population ++;
}

public Person(){
first_name = "";
surname = "";
location = "Hospital";
age = 0;
this.population ++;
}

public String say_my_name(){
return "My name is "+first_name+" "+surname+
". But you can just call me "+ first_name.substring(0,3);
}

public void walk(){
System.out.println("Walking...");
}

public void talk(String what_to_say){
System.out.println(first_name+" says: "+what_to_say);
}

public void talk(){
System.out.println("Hello there!");
}

public void talk(String what_to_say,boolean question){
if (question){
System.out.println("Umm... I don't know");
}
else {
System.out.println("*Why call this function then (to myself)...*\n"+what_to_say);
}

}

public static String getGenusSpecies(){
return genus_species;
}

public static void setGenusSpecies(String value){
genus_species = value;
}
}


//JavaApplication1.java
package javaapplication1;

import java.util.HashSet;

public class JavaApplication1 {

public static void main(String[] args) {
Person person = new Person();
Person person2 = new Person();

System.out.println(Person.population);
//2

System.out.println(Person.PLANET);
//Earth

System.out.println(Person.getGenusSpecies());
//Homo sapiens
Person.setGenusSpecies("Intelligenus humanus");
System.out.println(Person.getGenusSpecies());
//Intelligenus humanus

}
}

End…

This brings us to the end of our OOP basics. Hope this gives a broader perspective on Object Oriented Programming.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s