I want to create a method that uses dot(.) operator while calling in JAVA

I wanted to create a method that uses . while calling.

String A="Alpha",B="Beta"; A.compareTo(B);

So how to access A to define the method compareTo(). I can access the String B in definition of compareTo(), but to compare B with I need A as well. How can I access A. In the definition of the method.

-------------Problems Reply------------

String already has a compareTo.

If you're talking about adding new methods to existing classes without access to the class's source, like C#'s extension methods, that doesn't exist in Java.

Let me guess, Do you want to use 'this' keyword.

'this' gives you access to the current object on which the method is invoked.

You CAN'T do this, because you can't edit a pee-compiled class. What you want is to create your own object with a constructor (look for Java Class and Constructor). The method A.split(), for example, I a method defined in the String class. But string is final, so you can't create a class which extends String. Simply, you can't do it with a string.

Read something about classes and constructors here https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Category:java Views:8 Time:2019-02-28
Tags: java

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.167 (s). 11 q(s)