3

I am learning java on tutorials point. There the syntax of copyValueOf method is as follows.

public static String copyValueOf(char[] data)

It says that the method is static that means it has to be called with class name. But the program here works fine where it is called with respect to object why is it so,

public class Test {

  public static void main(String args[]) {
    char[] Str1 = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
    String Str2 = "";
    Str2 = Str2.copyValueOf( Str1 );
    System.out.println("Returned String: " + Str2);
  }

}

Also, I feel it redundant as the above 2 lines 4 and 5 be replaced with this

String Str2 = new String(Str1);

So, tell me where exactly it is useful.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • Because static methods can be called on an instance. The opposite is not true. – Federico klez Culloca Jun 26 '18 at 08:23
  • 6
    Static means the method is not related to instance but to class. You can either call it from the object or from the class, this change nothing. The only change is it's unclear you're calling a static method if you call it from the object and not from the class, so it's recommanded to call it from class – vincrichaud Jun 26 '18 at 08:24
  • In Java, unlike C/C++ an array of characters is not the same as a String. – Dragonthoughts Jun 26 '18 at 08:24
  • 3
    This is probably an educational example. Not "useful" production code. It seems it got you to ask the questions it wanted you to ask. – timgeb Jun 26 '18 at 08:26
  • To illustrate my comment, in your code it's unclear what it does when you write `Str2.copyValueOf(Str1)`. Because calling a method on a object, reader will expect this will do somthing with the object, or return a property of the object, but it's not the case. If you write `String.copyValueOf(Str1)`, the reader know you call a static method that is related with String but that won't access or modify an instance, here the reader expect this method to return something related only to Str1 – vincrichaud Jun 26 '18 at 08:32
  • Not directly related to your question, but it's strongly recommanded to follow some naming conventions. Variable should always start with a lower case letter, to avoid confusion between class and object. So in your case use `String str2` – vincrichaud Jun 26 '18 at 08:35
  • [Possible duplicate](https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method). – user202729 Jun 26 '18 at 15:06
  • You're asking multiple questions at once: (1) why can static method be called with object (2) when is `String.copyValueOf`useful. You should split it, otherwise your question may be closed as "too broad" according to [this meta post](https://meta.stackoverflow.com/q/267058). – user202729 Jun 26 '18 at 15:08

1 Answers1

1

Static means the method is not related to instance but to class. You can either call it from the object or from the class, this change nothing. The only change is it's unclear you're calling a static method if you call it from the object and not from the class, so it's recommended to call it from class

To illustrate this, in your code it's unclear what it does when you write str2.copyValueOf(str1). Because calling a method on a object, reader will expect this will do something with the object, or return a property of the object, but it's not the case. If you write String.copyValueOf(str1), the reader know you call a static method that is related with String but that won't access or modify an instance, here the reader expect this method to return something related only to str1.


Static method is useful in this case cause it will prevent you from initializing an object when you don't have to.

String str2 = "";
str2 = str2.copyValueOf(Str1);

Here you create 2 objects. First an empty String. Then a second string with the value of str1. The first initialization is useless, because you will drop the first String object just after creating it. It uses memory for nothing. So you can remove the initialisation.

String str2;
str2 = str2.copyValueOf(str1);

The problem here is that since you do not initialize str2, the system define it as null. So you can't call a method on it. str2.copyValueOf(str1); will throw a NullPointerException. But fortunately the method is static, so we can call it directly from the class. The code become

String str2;
str2 = String.copyValueOf(str1);

Note : this could be reduce in one line, but this change nothing in term of what happen at the execution : String str2 = String.copyValueOf(str1);


There is no difference between String str2 = String.copyValueOf(str1); and String str2 = new String(str1);. Since the method copyValueOf(char[]) just call the constructor String(char[]). From OpenJDK 7 line 2919 :

 /**
 * Returns a String that represents the character sequence in the
 * array specified.
 *
 * @param   data   the character array.
 * @return  a <code>String</code> that contains the characters of the
 *          character array.
 */
public static String copyValueOf(char data[]) {
    return new String(data);
}
vincrichaud
  • 2,218
  • 17
  • 34