DWR instantiate a class every time a method is called - java

I am trying to pass an arraylist that will be populated in method A of Class X and tehn I want to use teh same arraylist in method B of the same class. But since I am using DWR to access my java classes from JavaScripts, every time I am calling a method it is instantiates the class again and I loose the data inside the Arraylist in the process.
I am wondering if there is a work around or a formal way of passing the array list.
Thanks!

Related

Is it possible for a constructor to call itself?

Is it possible for a constructor to call a class which is related to it? For example (in the code below) I am trying to create a new clock with ClockTimer and call the AnalogClock construct which has a ClockTimer parameter type. It doesn't seem to work because it gives me an error, but creating a new AnalogClock with the ClockTimer parameter in the ClockFrame class appears to work. What do I need to parse in AnalogClock (in the ClockApp) in order for it to run?
From the outside, it looks like the "analogClock" object needs the "timer" object to exist first, and the "timer" object needs the "analogClock" object to exist first.
In reality, thought, "timer" doesn't do anything with the "analogClock" ("clockFrame" inside "ClockTimer"), so it actually doesn't need it.
I'd suggest the following:
Remove the "clockFrame" parameter and field inside "ClockTimer"
Create the "timer" object first
Create the "analogClock" object second, and pass "timer" to it

Instance and Static Methods in Java

I have a Doubt and i can't find it on google or on Youtube, so I am Asking here.
As we know,
Instance methods can call other Instance Methods directly without objects in the same class. However we need objects to call Instance method in a Static method.
I want to ask why is it so? How does Instance method calls other instance method directly without object but need an object to get called inside a Static method?
Because instance methods already have the object. You can access it with the this keyword. When you call an instance method in an instance method, it calls it on this unless you specifically call it on another instance. In a static method you don't have an object, so there is no this object to call it on. So unless you specify an instance there's no default to call it on.
Why doesn't the language force you to call it on this rather than assume? Convenience, it allows you to type a bit less. Its a common feature in other object oriented languages as well. There are languages that won't do it and require you to specify the object at all times.

Is Array Serializable/Parcelable if object is Serializable/Parcelable

I am looking to pass an array of object from activity to activity in Android. I understand that the optimal way to do this is having your passed objects implement Parcelable or Serializable, as explained here.
However, if I am passing an array of these objects, will this still work, considering you are now passing an array of objects that implement those interfaces, rather than the object itself? If not, would I instead need to extend a class such as ArrayList and implement these interfaces, and pass that 'array' object instead?
As I understand it, calling serialize on an array causes the array to recursively call serialize on its members. However, it appears there may be a bug affecting this in Android versions prior to 5.0.1 https://stackoverflow.com/a/28720450/1541763
It seems that parceling an array follows this logically, but that unmarhsalling is a little more complicated: https://stackoverflow.com/a/10781119/1541763

Single method class - pass dependcies to constructor or method

I have a very simple class that has a single method.
The method needs two objects passed to it to prepare an output and return the output.
Should the class be instantiated with these two objects passed to the constructor, or should those be passed to method when being called?
Currently I can see that the method will always be called only once per request and always with the same objects passed to it.
There are a lot of factors involved, but in general, the constructor should take arguments for configuration (if any configuration is needed), and the actual input should be passed to a method. Methods should be the "verbs" or "actions" in an object-oriented design, while classes should be the "nouns" or "machines."
Simply buying or building a toaster doesn't result in toast. Someone creates the toaster (think of that as constructing an object), then they or someone else executes an operation with it to make toast (think of that as calling a method).
So, in the absence of more information, I would recommend passing the objects to the method, not the constructor. Even if it's the same objects being passed every time.

Is it possible to make a list of classes without instantiating them as actual objects?

I have a set of classes in my program which can be instantiated. Each class has a static method associated with it which updates a particular aspect of that class.
At run time the program chooses to use a number of the classes from the set.
Multiple versions of these classes are then instantiated as objects as the program runs.
Throughout the program there are key points where I need to call the static methods associated with the various classes but only the ones in use that the program picked at run time.
Is it possible for me to make a list/array containing the classes in use without instantiating them? So that I can then just go through that list and call the static method on each class?
The only other way I have found of doing it is instantiating one object for each class in use and storing them in an array and calling the methods from there. This seems a bit inefficient and not very elegant.
Any help is greatly appreciated.
You could use reflection to do something like this:
List <Class<?>> classes = new ArrayList<Class<?>>();
// alternatively, classes.add(com.foo.MyClass1.class);
classes.add(Class.forName("com.foo.MyClass1"));
classes.add(Class.forName("com.foo.MyClass2"));
// ...etc
Method m = classes.get(0).getDeclaredMethod("staticFunction");
m.invoke(null); // pass in an instance of the class if this is an instance method
Check out the javadoc for Class and Method.
I'm not sure why you want to avoid instantiation, but if the only thing you are doing is calling static methods, there is not reason to create objects. As for your question, you can create an Array of Classes without any problem. Remember that MyClass.class is actually an object of class Class, so this would work :
Class[] classes = new Class[] {MyClass1.class, MyClass2.class};
You can then use your logic to select some of them. When done, you can instantiate them using reflection :
classes[0].newInstance();
Hope it helps, if not, please precise your question. Note that the class objects themselves will be created (not sure you can prevent the class loader to load them).
You can have a list of Class objects such as
List<Class> myClasses = new ArrayList<Class>();
To add an item to the list you code something like myClasses.add(Class1.class);
That's one way to store a list of classes, without needing to create an instance of each one.

Categories

Resources