{"id":43667,"date":"2015-09-18T21:43:46","date_gmt":"2015-09-18T18:43:46","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=43667"},"modified":"2023-12-06T12:37:36","modified_gmt":"2023-12-06T10:37:36","slug":"using-methods-common-to-all-objects","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html","title":{"rendered":"Using methods common to all objects"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">Advanced Java<\/a>.<\/p>\n<p>This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">here<\/a>!<\/em><\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#methods\">2. Methods equals and hashCode<\/a><\/dt>\n<dt><a href=\"#toString\">3. Method toString<\/a><\/dt>\n<dt><a href=\"#clone\">4. Method clone<\/a><\/dt>\n<dt><a href=\"#equals\">5. Method equals and == operator<\/a><\/dt>\n<dt><a href=\"#helper\">6. Useful helper classes<\/a><\/dt>\n<dt><a href=\"#code\">7. Download the Source Code<\/a><\/dt>\n<dt><a href=\"#code\">8. What&#8217;s next<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>From <strong>part 1<\/strong> of the tutorial, <strong><a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-to-create-and-destroy-objects\/\">How to create and destroy objects<\/a><\/strong>, we already know that Java is an object-oriented language (however, not a pure object-oriented one). On top of the Java class hierarchy sits the <code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Object.html\">Object<\/a><\/code> class and every single class in Java implicitly is inherited from it. As such, all classes inherit the set of methods declared in <code>Object<\/code>class, most importantly the following ones:<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"227\"><strong>Method<\/strong><\/td>\n<td width=\"397\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>protected Object clone()<\/code><\/td>\n<td width=\"397\">Creates and returns a copy of this object.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>protected void finalize()<\/code><\/td>\n<td width=\"397\">Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. We have discussed finalizers in the <strong>part 1<\/strong> of the tutorial, <strong>How to create and destroy objects<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>boolean equals(Object obj)<\/code><\/td>\n<td width=\"397\">Indicates whether some other object is \u201cequal to\u201d this one.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>int hashCode()<\/code><\/td>\n<td width=\"397\">Returns a hash code value for the object.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>String toString()<\/code><\/td>\n<td width=\"397\">Returns a string representation of the object.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>void notify()<\/code><\/td>\n<td width=\"397\">Wakes up a single thread that is waiting on this object&#8217;s monitor. We are going to discuss this method in the <strong>part 9<\/strong> of the tutorial, <strong>Concurrency best practices<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>void notifyAll()<\/code><\/td>\n<td width=\"397\">Wakes up all threads that are waiting on this object&#8217;s monitor. We are going to discuss this method in the <strong>part 9<\/strong> of the tutorial, <strong>Concurrency best practices<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"227\"><code>void wait()<\/code>, <code>void wait(long timeout)<\/code>, <code>void wait(long timeout, int nanos)<\/code><\/td>\n<td width=\"397\">Causes the current thread to wait until another thread invokes the <code>notify()<\/code> method or the <code>notifyAll()<\/code> method for this object. We are going to discuss these methods in the <strong>part 9<\/strong> of the tutorial, <strong>Concurrency best practices<\/strong>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">Table 1<\/p>\n<\/div>\n<p>In this part of the tutorial we are going to look at <code>equals<\/code>,<code>hashCode<\/code>,<code>toString<\/code> and <code>clone<\/code> methods, their usage and important constraints to keep in mind.<\/p>\n<h2><a name=\"methods\"><\/a>2. Methods equals and hashCode<\/h2>\n<p>By default, any two object references (or class instance references) in Java are equal only if they are referring to the same memory location (reference equality). But Java allows classes to define their own equality rules by overriding the <code>equals()<\/code> method of the <code>Object<\/code> class. It sounds like a powerful concept, however the correct <code>equals()<\/code> method implementation should conform to a set of rules and satisfy the following constraints:<\/p>\n<ul>\n<li><strong>Reflexive<\/strong>. Object <strong>x<\/strong> must be equal to itself and <strong>equals(x)<\/strong> must return <strong>true<\/strong>.<\/li>\n<li><strong>Symmetric<\/strong>. If <strong>equals(y)<\/strong> returns <strong>true<\/strong> then <strong>y.equals(x)<\/strong> must also return <strong>true<\/strong>.<\/li>\n<li><strong>Transitive<\/strong>. If <strong>equals(y)<\/strong> returns <strong>true<\/strong> and <strong>y.equals(z)<\/strong> returns <strong>true<\/strong>, then <strong>x.equals(z)<\/strong> must also return <strong>true<\/strong>.<\/li>\n<li><strong>Consistent<\/strong>. Multiple invocation of <strong>equals()<\/strong> method must result into the same value, unless any of the properties used for equality comparison are modified.<\/li>\n<li><strong>Equals To Null<\/strong>. The result of <strong>equals(null)<\/strong> must be always <strong>false<\/strong>.<\/li>\n<\/ul>\n<p>Unfortunately, the Java compiler is not able to enforce those constraints during the compilation process. However, not following these rules may cause very weird and hard to troubleshoot issues. The general advice is this: if you ever are going to write your own <code>equals()<\/code> method implementation, think twice if you really need it. Now, armed with all these rules, let us write a simple implementation of the <code>equals()<\/code> method for the <code>Person<\/code> class.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.advanced.objects;\n\npublic class Person {\n    private final String firstName;\n    private final String lastName;\n    private final String email;\n    \n    public Person( final String firstName, final String lastName, final String email ) {\n        this.firstName = firstName;\n        this.lastName = lastName;\n        this.email = email;\n    }\n    \n    public String getEmail() {\n        return email;\n    }\n    \n    public String getFirstName() {\n        return firstName;\n    }\n    \n    public String getLastName() {\n        return lastName;\n    }\n\n    \/\/ Step 0: Please add the @Override annotation, it will ensure that your\n    \/\/ intention is to change the default implementation.\n    @Override\n    public boolean equals( Object obj ) {\n        \/\/ Step 1: Check if the 'obj' is null\n        if ( obj == null ) {\n            return false;\n        }\n        \n        \/\/ Step 2: Check if the 'obj' is pointing to the this instance\n        if ( this == obj ) {\n            return true;\n        }\n        \n        \/\/ Step 3: Check classes equality. Note of caution here: please do not use the \n        \/\/ 'instanceof' operator unless class is declared as final. It may cause \n        \/\/ an issues within class hierarchies.\n        if ( getClass() != obj.getClass() ) {\n            return false;\n        }\n        \n        \/\/ Step 4: Check individual fields equality\n        final Person other = (Person) obj;\n        if ( email == null ) {\n            if ( other.email != null ) {\n                return false;\n            } \n        } else if( !email.equals( other.email ) ) {\n            return false;\n        }\n        \n        if ( firstName == null ) {\n            if ( other.firstName != null ) {\n                return false;\n            } \n        } else if ( !firstName.equals( other.firstName ) ) {\n            return false;\n        }\n            \n        if ( lastName == null ) {\n            if ( other.lastName != null ) {\n                return false;\n            }\n        } else if ( !lastName.equals( other.lastName ) ) {\n            return false;\n        }\n        \n        return true;\n    }        \n}\n<\/pre>\n<p>It is not by accident that this section also includes the <code>hashCode()<\/code> method in its title. The last, but not least, rule to remember: whenever you override <code>equals()<\/code> method, always override the <code>hashCode()<\/code> method as well. If for any two objects the <code>equals()<\/code> method returns <strong>true<\/strong>, then the <code>hashCode()<\/code> method on each of those two objects must return the same integer value (however the opposite statement is not as strict: if for any two objects the <code>equals()<\/code> method returns <strong>false<\/strong>, the <code>hashCode()<\/code> method on each of those two objects may or may not return the same integer value). Let us take a look on <code>hashCode()<\/code> method for the <code>Person<\/code> class.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">\/\/ Please add the @Override annotation, it will ensure that your\n\/\/ intention is to change the default implementation.\n@Override\npublic int hashCode() {\n    final int prime = 31;\n        \n    int result = 1;\n    result = prime * result + ( ( email == null ) ? 0 : email.hashCode() );\n    result = prime * result + ( ( firstName == null ) ? 0 : firstName.hashCode() );\n    result = prime * result + ( ( lastName == null ) ? 0 : lastName.hashCode() );\n        \n    return result;\n}      \n<\/pre>\n<p>To protect yourself from surprises, whenever possible try to use <code>final<\/code> fields while implementing <code>equals()<\/code> and <code>hashCode()<\/code>. It will guarantee that behavior of those methods will not be affected by the field changes (however, in real-world projects it is not always possible).<\/p>\n<p>Finally, always make sure that the same fields are used within implementation of <code>equals()<\/code> and <code>hashCode()<\/code> methods. It will guarantee consistent behavior of both methods in case of any change affecting the fields in question.<\/p>\n<h2><a name=\"toString\"><\/a>3. Method toString<\/h2>\n<p>The <code>toString()<\/code> is arguably the most interesting method among the others and is being overridden more frequently. Its purpose is it to provide the string representation of the object (class instance). The properly written <code>toString()<\/code> method can greatly simplify debugging and troubleshooting of the issues in real-live systems.<\/p>\n<p>The default <code>toString()<\/code> implementation is not very useful in most cases and just returns the full class name and object hash code, separated by <code>@<\/code>, f.e.:<\/p>\n<pre class=\"brush:bash\">com.javacodegeeks.advanced.objects.Person@6104e2ee<\/pre>\n<p>Let us try to improve the implementation and override the <code>toString()<\/code> method for our <strong>Person<\/strong> class example. Here is a one of the ways to make <code>toString()<\/code> more useful.<\/p>\n<pre class=\"brush:java\">\/\/ Please add the @Override annotation, it will ensure that your\n\/\/ intention is to change the default implementation.\n@Override\npublic String toString() {\n    return String.format( \"%s[email=%s, first name=%s, last name=%s]\", \n        getClass().getSimpleName(), email, firstName, lastName );\n}\n<\/pre>\n<p>Now, the <code>toString()<\/code> method provides the string version of the <code>Person<\/code> class instance with all its fields included. For example, while executing the code snippet below:<\/p>\n<pre class=\"brush:java\">final Person person = new Person( \"John\", \"Smith\", \"john.smith@domain.com\" );\nSystem.out.println( person.toString() );\n<\/pre>\n<p>The following output will be printed out in the console:<\/p>\n<pre class=\"brush:bash\">Person[email=john.smith@domain.com, first name=John, last name=Smith]<\/pre>\n<p>Unfortunately, the standard Java library has a limited support to simplify <code>toString()<\/code> method implementations, notably, the most useful methods are <code>Objects.toString()<\/code>, <code>Arrays.toString() \/ Arrays.deepToString()<\/code>. Let us take a look on the <code>Office<\/code> class and its possible <code>toString()<\/code> implementation.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.advanced.objects;\n\nimport java.util.Arrays;\n\npublic class Office {\n    private Person[] persons;\n\n    public Office( Person ... persons ) {\n         this.persons = Arrays.copyOf( persons, persons.length );\n    }\n    \n    @Override\n    public String toString() {\n        return String.format( \"%s{persons=%s}\", \n            getClass().getSimpleName(), Arrays.toString( persons ) );\n    }\n    \n    public Person[] getPersons() {\n        return persons;\n    }\n}\n<\/pre>\n<p>The following output will be printed out in the console (as we can see the <code>Person<\/code> class instances are properly converted to string as well):<\/p>\n<pre class=\"brush:bash\">Office{persons=[Person[email=john.smith@domain.com, first name=John, last name=Smith]]}<\/pre>\n<p>The Java community has developed a couple of quite comprehensive libraries which help a lot to make <code>toString()<\/code> implementations painless and easy. Among those are <code><a href=\"http:\/\/code.google.com\/p\/guava-libraries\/\">Google Guava's<\/a><\/code> <a href=\"http:\/\/docs.guava-libraries.googlecode.com\/git\/javadoc\/com\/google\/common\/base\/Objects.ToStringHelper.html\">Objects.toStringHelper<\/a> and <code><a href=\"http:\/\/commons.apache.org\/proper\/commons-lang\/\">Apache Commons Lang<\/a><\/code> <a href=\"http:\/\/commons.apache.org\/proper\/commons-lang\/apidocs\/org\/apache\/commons\/lang3\/builder\/ToStringBuilder.html\">ToStringBuilder<\/a>.<br \/>\n[ulp id=&#8217;w6F4W4SAMiyTapBF&#8217;]<br \/>\n&nbsp;<\/p>\n<h2><a name=\"clone\"><\/a>4. Method clone<\/h2>\n<p>If there is a method with a bad reputation in Java, it is definitely <code>clone()<\/code>. Its purpose is very clear \u2013 return the exact copy of the class instance it is being called on, however there are a couple of reasons why it is not as easy as it sounds.<\/p>\n<p>First of all, in case you have decided to implement your own <code>clone() <\/code>method, there are a lot of conventions to follow as stated in <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Object.html#clone()\">Java documentation<\/a>. Secondly, the method is declared <code>protected<\/code> in <code>Object<\/code> class so in order to make it visible, it should be overridden as <code>public<\/code> with return type of the overriding class itself. Thirdly, the overriding class should implement the <code>Cloneable<\/code> interface (which is just a marker or mixin interface with no methods defined) otherwise <code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/CloneNotSupportedException.html\">CloneNotSupportedException<\/a><\/code> exception will be raised. And lastly, the implementation should call <code>super.clone()<\/code> first and then perform additional actions if needed. Let us see how it could be implemented for our sample <code>Person<\/code> class.<\/p>\n<pre class=\"brush:java\">public class Person implements Cloneable {\n    \/\/ Please add the @Override annotation, it will ensure that your\n    \/\/ intention is to change the default implementation.\n    @Override\n    public Person clone() throws CloneNotSupportedException {\n        return ( Person )super.clone();\n    }\n}\n<\/pre>\n<p>The implementation looks quite simple and straightforward, so what could go wrong here? Couple of things, actually. While the cloning of the class instance is being performed, no class constructor is being called. The consequence of such a behavior is that unintentional data sharing may come out. Let us consider the following example of the <code>Office<\/code> class, introduced in previous section:<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.advanced.objects;\n\nimport java.util.Arrays;\n\npublic class Office implements Cloneable {\n    private Person[] persons;\n\n    public Office( Person ... persons ) {\n         this.persons = Arrays.copyOf( persons, persons.length );\n    }\n\n    @Override\n    public Office clone() throws CloneNotSupportedException {\n        return ( Office )super.clone();\n    }\n    \n    public Person[] getPersons() {\n        return persons;\n    }\n}\n<\/pre>\n<p>In this implementation, all the clones of the <code>Office<\/code> class instance will share the same persons array, which is unlikely the desired behavior. A bit of work should be done in order to make the <code>clone()<\/code> implementation to do the right thing.<\/p>\n<pre class=\"brush:java\">@Override\npublic Office clone() throws CloneNotSupportedException {\n    final Office clone = ( Office )super.clone();\n    clone.persons = persons.clone();\n    return clone;\n}\n<\/pre>\n<p>It looks better now but even this implementation is very fragile as making the persons field to be <code>final<\/code> will lead to the same data sharing issues (as <code>final<\/code> cannot be reassigned).<\/p>\n<p>By and large, if you would like to make exact copies of your classes, probably it is better to avoid <code>clone()<\/code> \/ <code>Cloneable <\/code> and use much simpler alternatives (for example, copying constructor, quite familiar concept to developers with C++ background, or factory method, a useful construction pattern we have discussed in <strong>part 1<\/strong> of the tutorial, <strong>How to create and destroy objects<\/strong>).<\/p>\n<h2><a name=\"equals\"><\/a>5. Method equals and == operator<\/h2>\n<p>There is an interesting relation between Java <code>==<\/code> operator and <strong>equals()<\/strong> method which causes a lot of issues and confusion. In most cases (except comparing primitive types), <strong>==<\/strong> operator performs referential equality: it returns <strong>true<\/strong> if both references point to the same object, and <strong>false<\/strong> otherwise. Let us take a look on a simple example which illustrates the differences:<\/p>\n<pre class=\"brush:java\">final String str1 = new String( \"bbb\" );\nSystem.out.println( \"Using == operator: \" + ( str1 == \"bbb\" ) );\nSystem.out.println( \"Using equals() method: \" + str1.equals( \"bbb\" ) );\n<\/pre>\n<p>From the human being prospective, there are no differences between str1==&#8221;bbb&#8221; and str1.equals(&#8220;bbb&#8221;): in both cases the result should be the same as str1 is just a reference to &#8220;bbb&#8221; string. But in Java it is not the case:<\/p>\n<pre class=\"brush:java\">Using == operator: false\nUsing equals() method: true\n<\/pre>\n<p>Even if both strings look exactly the same, in this particular example they exist as two different string instances. As a rule of thumb, if you deal with object references, always use the <code>equals()<\/code> or <code>Objects.equals()<\/code> (see please next section <a href=\"#helper\">Useful helper classes<\/a> for more details) to compare for equality, unless you really have an intention to compare if object references are pointing to the same instance.<\/p>\n<h2><a name=\"helper\"><\/a>6. Useful helper classes<\/h2>\n<p>Since the release of Java 7, there is a couple of very useful helper classes included with the standard Java library. One of them is class <code><a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Objects.html\">Objects<\/a><\/code>. In particular, the following three methods can greatly simplify your own <code>equals()<\/code> and <code>hashCode()<\/code> method implementations.<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"265\"><strong>Method<\/strong><\/td>\n<td width=\"359\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"265\"><code>static boolean equals(Object a, Object b)<\/code><\/td>\n<td width=\"359\">Returns <strong>true<\/strong> if the arguments are equal to each other and <strong>false<\/strong> otherwise.<\/td>\n<\/tr>\n<tr>\n<td width=\"265\"><code>static int hash(Object... values)<\/code><\/td>\n<td width=\"359\">Generates a hash code for a sequence of input values.<\/td>\n<\/tr>\n<tr>\n<td width=\"265\"><code>static int hashCode(Object o)<\/code><\/td>\n<td width=\"359\">Returns the hash code of a non-null argument and 0 for a null argument.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">Table 2<\/p>\n<\/div>\n<p>If we rewrite <code>equals()<\/code> and <code>hashCode()<\/code> method for our <code>Person<\/code>\u2019s class example using these helper methods, the amount of the code is going to be significantly smaller, plus the code becomes much more readable.<\/p>\n<pre class=\"brush:java\">@Override\npublic boolean equals( Object obj ) {\n    if ( obj == null ) {\n        return false;\n    }\n        \n    if ( this == obj ) {\n        return true;\n    }\n        \n    if ( getClass() != obj.getClass() ) {\n        return false;\n    }\n        \n    final PersonObjects other = (PersonObjects) obj;\n    if( !Objects.equals( email, other.email ) ) {\n        return false;\n    } else if( !Objects.equals( firstName, other.firstName ) ) {\n        return false;            \n    } else if( !Objects.equals( lastName, other.lastName ) ) {\n        return false;            \n    }\n        \n    return true;\n}\n        \n@Override\npublic int hashCode() {\n    return Objects.hash( email, firstName, lastName );\n}      \n<\/pre>\n<h2><a name=\"code\"><\/a>7. Download the Source Code<\/h2>\n<ul>\n<li>You may download the source code here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/advanced-java-part-2.zip\">advanced-java-part-2<\/a><\/li>\n<\/ul>\n<h2><a name=\"next\"><\/a>8. What&#8217;s next<\/h2>\n<p>In this section we have covered the <code>Object<\/code> class which is the foundation of object-oriented programming in Java. We have seen how each class may override methods inherited from <code>Object<\/code> class and impose its own equality rules. In the next section we are going to switch our gears from coding and discuss how to properly design your classes and interfaces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out here! Table Of Contents &hellip;<\/p>\n","protected":false},"author":141,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[343,88,580,557,65],"class_list":["post-43667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-annotations","tag-concurrency","tag-generics","tag-reflection","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using methods common to all objects - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using methods common to all objects - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-18T18:43:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-06T10:37:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrey Redko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrey Redko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html\"},\"author\":{\"name\":\"Andrey Redko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\"},\"headline\":\"Using methods common to all objects\",\"datePublished\":\"2015-09-18T18:43:46+00:00\",\"dateModified\":\"2023-12-06T10:37:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html\"},\"wordCount\":1652,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Annotations\",\"Concurrency\",\"Generics\",\"Reflection\",\"Serialization\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html\",\"name\":\"Using methods common to all objects - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-18T18:43:46+00:00\",\"dateModified\":\"2023-12-06T10:37:36+00:00\",\"description\":\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/using-methods-common-to-all-objects.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Using methods common to all objects\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\",\"name\":\"Andrey Redko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"caption\":\"Andrey Redko\"},\"description\":\"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\\\/EE, C#\\\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).\",\"sameAs\":[\"http:\\\/\\\/aredko.blogspot.com\\\/\",\"http:\\\/\\\/ca.linkedin.com\\\/in\\\/aredko\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/andrey-redko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using methods common to all objects - Java Code Geeks","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html","og_locale":"en_US","og_type":"article","og_title":"Using methods common to all objects - Java Code Geeks","og_description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-18T18:43:46+00:00","article_modified_time":"2023-12-06T10:37:36+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Andrey Redko","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Andrey Redko","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html"},"author":{"name":"Andrey Redko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413"},"headline":"Using methods common to all objects","datePublished":"2015-09-18T18:43:46+00:00","dateModified":"2023-12-06T10:37:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html"},"wordCount":1652,"commentCount":5,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Annotations","Concurrency","Generics","Reflection","Serialization"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html","name":"Using methods common to all objects - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-18T18:43:46+00:00","dateModified":"2023-12-06T10:37:36+00:00","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Using methods common to all objects"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413","name":"Andrey Redko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","caption":"Andrey Redko"},"description":"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\/EE, C#\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).","sameAs":["http:\/\/aredko.blogspot.com\/","http:\/\/ca.linkedin.com\/in\/aredko"],"url":"https:\/\/www.javacodegeeks.com\/author\/andrey-redko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=43667"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43667\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=43667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=43667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=43667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}