Reputation: 121
I'm quite the programming beginner. I'm learning about methods and I want to know the difference between those two. Why can't I use my self made method the same way text.length(); is being used?
text.length();
text.selfMadeMethod(); instead of selfMadeMethod(text);
Edit: It seems miss wrote my example above, I'm sorry. I'll post the exercise I was working on so it can show more clearly what I tried to do.
import java.util.Scanner;
public class ReversingText {
public static String reverse(String text) {
int x = text.length() -1;
String reverse = "";
while (x >= 0) {
reverse += text.charAt(x);
x--;
}
return reverse;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type in your text: ");
String text = reader.nextLine();
System.out.println("In reverse order: " + reverse(text));
System.out.println("In reverse order: " + text.reverse());
}
}
The last print statement was just me trying that, the exercise didn't ask me to do it. Again I'm sorry if this is either a stupid question or unclear, thanks.
Upvotes: 0
Views: 187
Reputation: 646
When you call on a method by placing it AFTER the class/instance name, this indicates you are calling on a method that belongs to that class. Some examples:
text.length()
calls the method length()
in class String
(which text
is an instance of), using the values assigned to text
.Math.random()
calls the static method random()
in class Math
.The class which the method belongs to must be specified before the name of the method itself. If no class is specified, it is assumed to belong to the same class which it is called in.
This is why, in your example, it works the first way but not the second. reverse(text)
calls on the reverse(String)
method in the current class, passing text
to it as an argument. text.reverse()
calls on the reverse()
method in class String
, which does not exist.
Upvotes: 1
Reputation: 15634
Why can't I use my self made method the same way text.length(); is being used?
because you need an object to call a method on.
But you did not create an object of your class ReversingText
.
An object is created by invoking the new
operator followed by a class name (more precisely a constructor of the class)
new ReversingText();
But the method you wrote has the static
keyword which means it is accessible via the class name directly:
ReversingText.reverse("some string");
you don't need to write that in your code because you are in the same class where you can omit the class name.
if you want to use your own method like text.length()
you have to do 3 things:
static
key word from the method signature.create an instance of ReversingText
and store it in a variable in main:
ReversingText reversingText = new ReversingText();
call your method on that variable:
reversingText.reverse("some string");
Upvotes: 1
Reputation:
This is about the semantics of java.
public class SomeClass{
public void someMethod(){
}
}
The above code basically means SomeClass
has a method someMethod
that any instance can use in the following way:
SomeClass s = new SomeClass();
s.someMethod();
someMethod
is called an instance-method of SomeClass
. This means someMethod
is part of any instance of the class SomeClass
.
On the other hand
public void anotherMethod(SomeClass s){
}
means anotherMethod
accepts an instance of class SomeClass
as parameter and could be called like this:
anotherMethod(new SomeClass());
Note the difference between "instance method" and "parameter".
Classes are immutable from the outside. This means the only place where you can add methods to a class is directly within the definition of the respective class.
Upvotes: 2
Reputation: 27823
Alas you cannot reopen classes in Java.
text
is a string and you can only send messages to a string that are defined in the String
class. Alas you cannot the String
class extend it with your own functions. This is an unfortunate limitation of Java. Other languages like Ruby allow classes to be reopened.
The common way work around this limitation is to create a StringUtils
class with a static reverse
function and then use it as follows
StringUtils.reverse(text)
Upvotes: 1
Reputation: 792
You are asking about a very well known language feature, aka Pimp My Library. Some languages like Scala, Kotlin, C#,... already have this syntax. But unfortunately, Java does not have it just yet.
Upvotes: 0
Reputation: 201537
Because Java String
has String.length()
(and doesn't have a reverse(String)
method), and it is a final
class (that means you cannot sub-class it to add custom methods). It is worth mentioning, but not directly to your question, that there is already a StringBuilder.reverse()
method.
Upvotes: 3