1

I am new to the concepts of oop and therefore what I write have may have bad assumptions. Please correct me where appropriate.

In some code I saw a function takes in an argument in the form of int number. Then, in the function the following line is written outputString = Integer.toString( number );

I interpreted this as go the the class Integer, find a method called toString() and feed this the int called number and set the output of that to outputString. Since the syntax is class.method(arg) I assumed that toString() is a static function.

I wanted to test if it was static. I rewrote the code with the argument as Integer number and then did outputString = number.toString(); and it worked. This has the form obj.method(arg) which I had assumed would only work for non static methods.

I am unsure what conclusion to draw from this or if I have made a wrong assumption. Thanks.

Bloop
  • 53
  • 7

5 Answers5

3

If you're talking about the class Integer, it has two methods toString, one static and the other one is not static.

Integer a = 5;
a.toString() // Is valid
Integer.toString(5); // Also is valid
b.GHILAS
  • 2,273
  • 1
  • 8
  • 16
  • This is called "method overloading". You can have multiple methods of the same name (differentiated by their parameter signatures) for the same class (and some of those can be static as well). – Thilo Nov 27 '19 at 09:11
  • @tobias_k exactly there is one with two arguments, I forget about it, thanks – b.GHILAS Nov 27 '19 at 09:12
  • Thank you billal! I had no idea you could have more than one method with the same name like that! Thanks Thilo will look more into this. – Bloop Nov 27 '19 at 09:43
0

There are 3 methods in java.lang.Integer class.

  1. public static String toString(int i);
  2. public static String toString(int i, int radix);
  3. public String toString();

In your first case, you have used the 1st one ( a static method). In your second case, you have used the 3rd one ( a non-static method).

cyrilantony
  • 274
  • 3
  • 14
0

I think you should know more information about inheritance in java. All classes extend Object class, that is why all classes have non-static toString() method and can override it. Class Integer have also static toString(int number) that returns String representation of int.

0

The Integer class has 2 methods as below;

public static String toString(int i) {
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        char[] buf = new char[size];
        getChars(i, size, buf);
        return new String(buf, true);
    }

AND

public String toString() {
        return toString(value);
    }

Both the methods are self explanatory. To Note second method calls the first one.

Integer.toString(1); // Calls method 1
Integer one = 1;
one.toString(); // Calls method 2
Ismail
  • 1,188
  • 13
  • 31
0

You may also want to lookup the Object's toString() method, which by the way gets inherited in every objects in java. This method is not static. The Integer's toString(int) method is only specific to the Integer class. This method is static (does not work on objects, but only on the Integer class). You will also want to check out the concepts of "method overriding", "method overloading" and "polymorphism". Here's a page containing almost exactly your question: https://www.geeksforgeeks.org/can-we-overload-or-override-static-methods-in-java/

Janos Vinceller
  • 1,208
  • 11
  • 25