Passing data from one class to another class - java

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

Related

Can we add a import statement to a class using Javassist

I want to call a method in different class in a different package using the lines injected through javassist. Is there any other way to call those methods directly without making them public static and call using the fully qualified classname? In here it describes about a importPackage() method. But it looks like it can be used only with classpool object where we try to obtain the required class file.
Imports are compile time only. If you want to invoke non-static methods in another class, you will need some means of acquiring the correct object instances to invoke against, such as:
Instantiating them
Some sort of object finder
Implements a shared collection that can be accessed statically that contains the objects.
One of these would need to be injected into your class so you can get the instances, and then you can wire in the actual invocation.
It might be helpful if you provided some code samples, perhaps how the code is now, and how you would like it to be.

How can I instantiate a Robotframework library class with a factory method?

I have a Java class which I want to import as a library in my robot file.
The problem is that this class is a singleton with a static method newInstance instead of ordinary public constructor.
Is there any way to tell RobotFramework to use newInstance instead of constructor to instantiate a library object?
Of course, I can use Call Method to get the desired "library" object and then again call its method, but this will disable all the magic and simplicity of direct using of library class methods as keywords.
Making the class a non-singleton is not a solution in my case.
No, according to the user guide, it is not possible. But you can define the library's scope bysetting ROBOT_LIBRARY_SCOPE accordingly.
Maybe this helps:
GLOBAL
Only one instance is created during the whole test execution and it is shared by all test cases and test suites. Libraries created from
modules are always global.

Where to put "general" functions in Android?

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.

How to programmatically know who (other java files/classes) is using my java class?

I want to get a list of all java class which are dependent on my class. Is there a library which exposes intended API? API is expected to return list of java classes using my java class.
You would probably use Reflection API..
They were specifically made for this kind of problems.. They allow to get information about classes at runtime..
You can get: -
All the methods
All the derived classes
All the variables.
And many more information..
You can see Class.getClasses() and Class.getDeclaredClasses()
See some more examples
Reflection can tell you whether a specific class extends your class, uses your class as a field, takes your class as a parameter, or returns your class from a method. However if the use of your class is method confined then reflection will not work.

what is the use of having public methods when the class is having a default access modifier?

as for my observation when the class itself is having default access modifier, what is the use of having public methods in it. the java compiler could have stopped using public methods in default class. is there any reason for that?
The non-public class might implement a public interface. This would mean that classes outside of the package could not create an instance of this class or create references of that type, but they would still be able to invoke methods on it if passed an instance.
For example, a public factory class might create an instance of an non-public class in its package and return it.
One reason: if your class implements some interface (or extends some abstract class with abstract public methods), then you may not reduce the visibility of those implemented methods.
It is a beautiful combination of Security and Usability packed in one.
I would mark a Class with default access if I want it to have a, well, package access (so that no other package can use it or better change the code) and marking a method public, I am making the method accessible to all other classes regardless of the package they belong to.
How does that help? A class which is secure enough to perform all the complex code implementation and usable enough to give the output to the user who wants to use it.
How can anyone use that? Well you write code to help them use it by creating a public class which extends this default class. You Instantiate this public Subclass in any package (after importing of-course) and this has all the methods marked public.
You have a class which does your magic which everyone can use without giving anyone else a hint of how you got it done!

Categories