Where to put "general" functions in Android? - java

I want to write a general function for my project that will be used across many different files and I don't want to have it in a particular class.
Where am I supposed to put it?
Inside a new package as a class?
Is this the only option?

Create a new package as util and create all common methods in there, for example:
Create package as com.xyz.util.
Create a class as Util in above package.
Then write your all common methods in this Util class.
You can write static methods in this class and access them as Util.method_name();.

In Java every method lives in a class. So yes, its the only option. A common approach is to use a utility class containing only static methods. You can use it from everywhere without creating any instances.

Related

Is there any convention to store methods not particularly related to any class? [java]

I am currently learning Java and, while making a project, I created some methods that do not suit logically in any given class but are useful in the whole context of the project.
The best example I have is a method that splits camelCase worlds like this:
splitCamelCase -> Split Camel Case.
I have thought about creating a new abstract class called Toolbox and storing those methods there, but I wonder if there is any convention or best practice regarding this topic.
It's not uncommon to have utility classes (commonly named SomethingUtils) when it just doesn't make sense to put a method in an existing class.
There's nothing inherently wrong with it, but if you find yourself having a lot of methods or utility classes, then your design might be a bit off and you're programming in a more procedural than object oriented way.
As mentioned in comments, you don't make it an abstract class. It's a class filled with static methods working entirely on the parameters passed to them.
As kayaman sir has said if you are having too many utility classes and method it means that you code is more procedural rather than object oriented.
Nut if you still want to have a class which is just used to provide some utility then you can have such a class in java , just put some static method in them.
One of the best example of such a class is java.lang.Math.
for example following code will work
class MyUtilityClass
{
private MyUtilityClass()
{
// no object creation will be allowed
}
// make as many static methods you want
}
You can create your ToolBox Class and then you declare it as a package. After that you can import your ToolBox at the beginning of classes you want to use the methods from that ToolBox.

Why don't we have to create object of System or Math classes in java and use them directly?

We use System.out.println without instantiating it or creating object of it. Same goes to Math class and many others (I guess). Is there something special about these classes? Can we use the classes and methods declared within those classes in same fashion? Please help.
You don't have to create objects for the System and Math classes because the methods and variables in those classes are static. This means that they belong to the class itself, not to instances of the class.
For reference see:
Understanding Class Members
Beyond Basic Arithmetic
This is something called 'static' method. In order to invoke static method, you do not need to have an instance of the class.
This also has other side effects such as non-existing 'this' and thus static methods cannot invoke instance methods.
This is mostly used for some sort of utility classes which are often stateless.
Math is a good example for it.
I suggest to read a bit about static methods and static in Java in general.
You don’t need to create object of System and Math class to use it because they have static methods. Static methods belong to the class and thus doesn’t require it to be instantiated.
Although, you can create its object and then also use those methods, but creating a class for static method is of no use.
Why don't we have to create object of System or Math classes in java and use them directly?
Because the methods of Math are declared as static methods, and because System.in / System.out / System.err are static variables.
Is there something special about these classes?
No. Any variables or methods that are declared as static will behave that way.
Can we use the classes and methods declared within those classes in same fashion?
I don't really understand what you are asking there. But, if you are asking if you can create an instance of Math or System so that you can do something like this:
Math myMath = new Math();
myMath.min(1, 2);
No, you can't. Neither of those classes has a public constructor, so you can't new them.
And if you could do that, it would be really bad style!
Reference:
Understanding Class Members
First,you cannot make an instance of the class Math,because it has only a single constructor and it's been marked private and you just can't make an instance of it from outside the class.
Snapshot of the source code of the class Math
Second,you don't need to do that.All of the methods in class Math are static,just use the class name and the dot operator and you can invoke any one of them.
System class can't instantiate/create object because this System class have private constructor.
And it's all members and methods are static, that can be accessible directly by Class name.
this simple and valid answer will help you.
We don't instantiate every other class or method because the JVM(Java Virtual Machine) already loads them into the project and hence, we can use these classes again and again. One such example is the main method. These classes/methods are already predefined for us so there is no need for us to instantiate such classes/methods because they are static.
You don't have to instantiate the object in order to use methods of the math class.
Because to use this methods we don't need object. We can directly invoke this.
These type of classes are called static. Here methods can directly invoked by the class itself.
They are already defined in the JVM. We don't need to instantiate to use methods of this class.

Extract static methods from main() file in java

I'm quite new to Java and wrote a little to-do list project.
At the beginning of the project I added a bunch of static methods to the file where my main() code is, and it got a little out of hand. I want to transfer these methods to another file.
Is there a proper way to do this, or do I just have to create some sort of Behaviour class for these methods, and then in main() create an instance of it to call it's methods?
You can extract these methods to a separate class (say FooUtils), and then in your main method you can call them using the class name - FooUtils.someStaticMethod()
Depending on what you have, it may make sense to group your methods into different classes, or to make them instance methods.

Allowing classes with different parents to extend class without multiple inheritance

My application has the following class:
MyTextField, which extends JTextField.
I, however, need the methods inside MyTextField in the following class:
MyPasswordField, which extends JPasswordField.
Multiple inheritance isn't allowed in Java, and I'd like to avoid to copy-paste 85% of the class because of this. How to overcome this "limitation"?
Use a helper class that defines the shared functionality. Declare a field in each of MyTextField and in MyPasswordField to hold an instance of the helper class. Relay calls to the helper as needed for the shared functionality, which then only needs to be coded once.
Why don't you use Composition instead of Inheritance.
Your MyPasswordField class can contain an instance variable to myTextField, then you can just use the method using your instance and it's methods.
Let me know if I am going on the wrong track and you expect something different.
You can achieve this with the help of a Helper class with static methods, so you don't need keep an instance of the helper class in your testfields classes.

Passing data from one class to another class

I am developing a utility class in Android 2.2 exposing various methods to Application.
Application can import my jar file(utility class) to invoke methods defined in utility class. Can somebody tell me how to pass/return data to application class from my utility class.
Note: utility class and application class is kept under different packages.
Passing Information to a Method or a Constructor
If you wants to use class of any other package in your current class then you will have to import that package in your class and then create object of its class and call its function.
http://leepoint.net/notes-java/language/10basics/import.html
EDIT: i found a same type of question :
Using utility classes in the android programming
It heavily depends on what kind of utility you're referring to. There are
1) utility classes that implement static methods. In that case you just call them directly using class name
2) utility classes methods that are not static - requires creating and possibly initializing an instance of that class. Then the instance is used to call those methods.
3) utility classes that can be accessed thru Context. then you can call getApplicationContext() and then you can get access to the utility classes

Categories