Skip to main content

Java Keywords (Part XXIV): native

Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const * float native super while Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers. This is the last chapter of the Java Keyword series. This is probably the keyword I have used the least. In my 20 year career as a software developer, I have used this keyword once, and that was to make some addition to legacy code. The keyword native is a method modifier . Basically, it is a keyword that can only be applied to methods. According to the Java Language Specification (JLS), A method that is native is implemented i...

Java Keywords (Part XVII): The default keyword

This article summarize the use of the keyword default. I suggest you review Java Keywords (Part V): Classes vs Interfaces and Java Keywords (Part IX): Switch Statements before proceeding.

Java keyword list

abstract continue for new switch
assert default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const* float native super while
Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers.
Up until Java 8 (released in March, 2014), the default keyword had a single purpose, which was to indicate the default case in a switch statement. To illustrate quickly

String color = "red";
switch(color) {
    case "red":
    case "white":
	case "blue":
    	System.out.println(color + " is a supported color.");
        break;
	default:
    	System.err.println(color + " is not supported.");
        break;
}
Basically, in this context, the default keyword is akin to the else block in an if/else flow-control structure. Please understand that the code inside the default case does not have to handle exceptional cases, just like the else block. It is simply to tell the program "do these things for all other cases" whatever "these things" are. It could be error handling.

The second use of the default keyword for default methods in interfaces; introduced in Java 8. At the time I wrote that article, I wanted to cover this topic. After much deliveration, I decided to wait and write another article (this one) where I could wrap up the use and cover both cases. The reason was to keep it short. Covering both cases would have made the article too long and probably too "all over the place."

Here is an example of a default method in a Java interface:


public interface Pet {
	void learnTrick(String trickName);
	void doTrick(String trickName);
	void giveName(String name);
    void respondByName(String trickName);
    void dressUp();
    
    default void identify() {
    	System.out.println("I am simply a pet.")
    }
}
You may ask, what is so special about this? Well, up until Java 8, interfaces could not contain concrete methods. Concrete methods are methods with a body. In contrast, as you might recall, an abstract method does not have a method body and classes implementing an interface must implement all of its methods. According to Oracle
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
You may ask, what does this mean exactly?
  • It means that code written for Java 7 or earlier will continue to work if adopting classes implemeting interfaces written for Java 8 containing default methods.
  • It means that code that was written for any Java version will not break if an existing interface is updated to include default methods.
    • Keep in mind is that a default method can be overridden by an implementing class, but it does not have to.
Another added benefit of using default methods is that implementing classes no longer need a common concrete parent implementing default behavior; thus simplifying class hierarchy. Since Java supports single inheritance of classes, this is a huge added benefit. For more on the topic of default methods, please visit the Default Methods Java tutorial article on Oracle Java website.

Next up, Part XVIII: The static keyword

Comments

Popular posts from this blog

Implementing Interfaces with Java Records

If you have not read my article on Java records and do not know about this topic, please read my blog titled " Customizing Java Records " first and then come back to this one. Now that you know how to customize Java records, implementing an interface using Java records should be very easy to understand. If you don't know about interfaces in Java, you can read more on my article about interfaces. The recipe for implementing an interface is simply an expansion of what you learned in my previous blog on how to customize a Java record. Following our Rectangle example, let's create an interface with the same two methods we used before. public interface Shape { double area(); double perimeter(); } Now, let's further customize the previous example by doing two things: Add implements Shape at the end of the record declaration (after the record constructor), and Add @Override to the existing methods to ensure these methods com...

Customizing Java Records

If you have not read my article on Java records and do not know about this topic, please read my blog titled " Java Keywords Addendum: The Java Record " first and then come back to this one. What is a customization of a record? A customization of a record is simply the addition of code inside the body of the class. Before proceeding further, let's recap important aspects of a Java Record: Java records are immutable Because of item 1 above, you cannot add new fields unless defined in the record constructor Java records already override: Object#equals(Object) and Object#hashCode() , and then override Object#toString() You could redefine overridden methods as part of your customization if you would like. For example, if you want a fancier implementation of the Object#toString() method, you could do so. Let's look at our first customization example. Using the example from my previous blog, public record Student(...

Object-Oriented Programming Basics: What is in a Class?

EDITORIAL NOTE : This article was published briefly back in 2016 and quickly set back to draft because I wasn't happy with its contents. It is a shame that it was taking me three years to revisit this topic and work on a new and improved version. At least, I'm hoping it will be to the liking you the reader. Keep in mind that the opening paragraph will still read as if I just wrote it for my (former) students at Texas Wesleyan. I started working on lecture on the topic of Object-Oriented (OO) Programming by gathering some material, old and new, when I realized this might be good and simple post for my second attempt at blogging. To be completely honest, in the 8 hours I spent collecting information and preparing material for this week's lecture, I realized I still made some of the mistakes I am about to blog about. I am actually hoping I can write a series of postings regarding Object-Oriented Programming (OOP). But to do so, I must start from the very beginning. ...